MCPcopy Create free account
hub / github.com/gavv/httpexpect / getJSONP

Method getJSONP

response.go:973–1018  ·  view source on GitHub ↗
(
	opChain *chain, method string, callback string, options ...ContentOpts,
)

Source from the content-addressed store, hash-verified

971)
972
973func (r *Response) getJSONP(
974 opChain *chain, method string, callback string, options ...ContentOpts,
975) interface{} {
976 if !r.checkContentOptions(opChain, options, "application/javascript") {
977 return nil
978 }
979
980 content, ok := r.getContent(opChain, method)
981 if !ok {
982 return nil
983 }
984
985 m := jsonp.FindSubmatch(content)
986
987 if len(m) != 3 || string(m[1]) != callback {
988 opChain.fail(AssertionFailure{
989 Type: AssertValid,
990 Actual: &AssertionValue{
991 string(content),
992 },
993 Errors: []error{
994 fmt.Errorf(`expected: JSONP body in form of "%s(<valid json>)"`,
995 callback),
996 },
997 })
998 return nil
999 }
1000
1001 var value interface{}
1002
1003 if err := json.Unmarshal(m[2], &value); err != nil {
1004 opChain.fail(AssertionFailure{
1005 Type: AssertValid,
1006 Actual: &AssertionValue{
1007 string(content),
1008 },
1009 Errors: []error{
1010 errors.New("failed to decode json"),
1011 err,
1012 },
1013 })
1014 return nil
1015 }
1016
1017 return value
1018}
1019
1020func (r *Response) checkContentOptions(
1021 opChain *chain, options []ContentOpts, expectedType string, expectedCharset ...string,

Callers 1

JSONPMethod · 0.95

Calls 4

checkContentOptionsMethod · 0.95
getContentMethod · 0.95
failMethod · 0.80
ErrorfMethod · 0.65

Tested by

no test coverage detected