GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — render/render_jsonp_test.go at v1 · caueasantos/render Expand file tree/ Copy pathrender_jsonp_test.goMore file actions 112 lines (85 loc) · 2.92 KB / Copy pathrender_jsonp_test.goFile metadata and controls112 lines (85 loc) · 2.92 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 package render import ( "math" "net/http" "net/http/httptest" "testing" ) type GreetingP struct { One string `json:"one"` Two string `json:"two"` } func TestJSONPBasic(t *testing.T) { render := New() var err error h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSONP(w, 299, "helloCallback", GreetingP{"hello", "world"}) }) res := httptest.NewRecorder() req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/foo", nil) h.ServeHTTP(res, req) expectNil(t, err) expect(t, res.Code, 299) expect(t, res.Header().Get(ContentType), ContentJSONP+"; charset=UTF-8") expect(t, res.Body.String(), "helloCallback({\"one\":\"hello\",\"two\":\"world\"});") } func TestJSONPRenderIndented(t *testing.T) { render := New(Options{ IndentJSON: true, }) var err error h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSONP(w, http.StatusOK, "helloCallback", GreetingP{"hello", "world"}) }) res := httptest.NewRecorder() req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/foo", nil) h.ServeHTTP(res, req) expectNil(t, err) expect(t, res.Code, http.StatusOK) expect(t, res.Header().Get(ContentType), ContentJSONP+"; charset=UTF-8") expect(t, res.Body.String(), "helloCallback({\n \"one\": \"hello\",\n \"two\": \"world\"\n});\n") } func TestJSONPWithError(t *testing.T) { render := New() var err error h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSONP(w, 299, "helloCallback", math.NaN()) }) res := httptest.NewRecorder() req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/foo", nil) h.ServeHTTP(res, req) expectNotNil(t, err) expect(t, res.Code, 500) } func TestJSONPCustomContentType(t *testing.T) { render := New(Options{ JSONPContentType: "application/vnd.api+json", }) var err error h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSONP(w, http.StatusOK, "helloCallback", GreetingP{"hello", "world"}) }) res := httptest.NewRecorder() req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/foo", nil) h.ServeHTTP(res, req) expectNil(t, err) expect(t, res.Code, http.StatusOK) expect(t, res.Header().Get(ContentType), "application/vnd.api+json; charset=UTF-8") expect(t, res.Body.String(), "helloCallback({\"one\":\"hello\",\"two\":\"world\"});") } func TestJSONPDisabledCharset(t *testing.T) { render := New(Options{ DisableCharset: true, }) var err error h := http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) { err = render.JSONP(w, http.StatusOK, "helloCallback", GreetingP{"hello", "world"}) }) res := httptest.NewRecorder() req, _ := http.NewRequestWithContext(ctx, http.MethodGet, "/foo", nil) h.ServeHTTP(res, req) expectNil(t, err) expect(t, res.Code, http.StatusOK) expect(t, res.Header().Get(ContentType), ContentJSONP) expect(t, res.Body.String(), "helloCallback({\"one\":\"hello\",\"two\":\"world\"});") } |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |