|
| 1 | +package resty |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestDecodeJSONWhenResponseBodyIsNull(t *testing.T) { |
| 12 | + r := &Response{ |
| 13 | + Body: io.NopCloser(bytes.NewReader([]byte("null"))), |
| 14 | + } |
| 15 | + r.wrapCopyReadCloser() |
| 16 | + err := r.readAll() |
| 17 | + assertNil(t, err) |
| 18 | + |
| 19 | + var result map[int]int |
| 20 | + err = decodeJSON(r.Body, &result) |
| 21 | + assertNil(t, err) |
| 22 | + assertNil(t, result) |
| 23 | +} |
| 24 | + |
| 25 | +func TestGetMethodWhenResponseIsNull(t *testing.T) { |
| 26 | + server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 27 | + w.Write([]byte("null")) |
| 28 | + })) |
| 29 | + |
| 30 | + client := New().SetRetryCount(3).EnableGenerateCurlCmd() |
| 31 | + |
| 32 | + var x any |
| 33 | + resp, err := client.R().SetBody("{}"). |
| 34 | + SetHeader("Content-Type", "application/json; charset=utf-8"). |
| 35 | + SetForceResponseContentType("application/json"). |
| 36 | + SetAllowMethodGetPayload(true). |
| 37 | + SetResponseBodyUnlimitedReads(true). |
| 38 | + SetResult(&x). |
| 39 | + Get(server.URL + "/test") |
| 40 | + |
| 41 | + assertNil(t, err) |
| 42 | + assertEqual(t, "null", resp.String()) |
| 43 | + assertEqual(t, nil, x) |
| 44 | +} |
| 45 | + |
| 46 | +func TestDecodeJSON(t *testing.T) { |
| 47 | + // Test single object |
| 48 | + jsonData := `{"name": "John", "age": 30}` |
| 49 | + reader := bytes.NewReader([]byte(jsonData)) |
| 50 | + var result map[string]any |
| 51 | + err := decodeJSON(reader, &result) |
| 52 | + assertNil(t, err) |
| 53 | + assertEqual(t, "John", result["name"]) |
| 54 | + assertEqual(t, float64(30), result["age"]) |
| 55 | + |
| 56 | + // Test multiple objects - should get the last one |
| 57 | + multipleJSON := `{"id": 1} |
| 58 | +{"id": 2} |
| 59 | +{"id": 3}` |
| 60 | + reader2 := bytes.NewReader([]byte(multipleJSON)) |
| 61 | + var result2 map[string]any |
| 62 | + err = decodeJSON(reader2, &result2) |
| 63 | + assertNil(t, err) |
| 64 | + assertEqual(t, float64(3), result2["id"]) |
| 65 | + |
| 66 | + // Test malformed JSON |
| 67 | + malformedJSON := `{"name": "John", "age":}` |
| 68 | + reader3 := bytes.NewReader([]byte(malformedJSON)) |
| 69 | + var result3 map[string]any |
| 70 | + err = decodeJSON(reader3, &result3) |
| 71 | + assertNotNil(t, err) |
| 72 | +} |
| 73 | + |
| 74 | +func TestWrapCopyReadCloser(t *testing.T) { |
| 75 | + testData := "Hello, World!" |
| 76 | + r := &Response{ |
| 77 | + Body: io.NopCloser(bytes.NewReader([]byte(testData))), |
| 78 | + } |
| 79 | + |
| 80 | + // Before wrapping, bodyBytes should be empty |
| 81 | + assertEqual(t, 0, len(r.bodyBytes)) |
| 82 | + |
| 83 | + r.wrapCopyReadCloser() |
| 84 | + |
| 85 | + // Read data - should trigger copy mechanism and transform to nopReadCloser |
| 86 | + data, err := io.ReadAll(r.Body) |
| 87 | + assertNil(t, err) |
| 88 | + assertEqual(t, testData, string(data)) |
| 89 | + assertEqual(t, testData, string(r.bodyBytes)) |
| 90 | + |
| 91 | + // Should now be nopReadCloser for unlimited reads |
| 92 | + _, ok := r.Body.(*nopReadCloser) |
| 93 | + assertEqual(t, true, ok) |
| 94 | + |
| 95 | + // Test unlimited reads |
| 96 | + data2, err := io.ReadAll(r.Body) |
| 97 | + assertNil(t, err) |
| 98 | + assertEqual(t, testData, string(data2)) |
| 99 | +} |
| 100 | + |
| 101 | +func TestMultipleJSONObjectsSupport(t *testing.T) { |
| 102 | + // Test multiple JSON objects with wrapCopyReadCloser |
| 103 | + jsonData := `{"first": 1} |
| 104 | +{"second": 2} |
| 105 | +{"third": 3}` |
| 106 | + |
| 107 | + r := &Response{ |
| 108 | + Body: io.NopCloser(bytes.NewReader([]byte(jsonData))), |
| 109 | + } |
| 110 | + r.wrapCopyReadCloser() |
| 111 | + |
| 112 | + // Should process all objects and get the last one |
| 113 | + var result map[string]any |
| 114 | + err := decodeJSON(r.Body, &result) |
| 115 | + assertNil(t, err) |
| 116 | + assertEqual(t, float64(3), result["third"]) |
| 117 | + |
| 118 | + // Should support unlimited reads and decoding |
| 119 | + var result2 map[string]any |
| 120 | + err = decodeJSON(r.Body, &result2) |
| 121 | + assertNil(t, err) |
| 122 | + assertEqual(t, float64(3), result2["third"]) |
| 123 | + |
| 124 | + // Test direct nopReadCloser usage |
| 125 | + nopReader := &nopReadCloser{ |
| 126 | + r: bytes.NewReader([]byte(jsonData)), |
| 127 | + resetOnEOF: true, |
| 128 | + } |
| 129 | + |
| 130 | + var result3 map[string]any |
| 131 | + err = decodeJSON(nopReader, &result3) |
| 132 | + assertNil(t, err) |
| 133 | + assertEqual(t, float64(3), result3["third"]) |
| 134 | +} |
0 commit comments