|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "net/http" |
| 6 | + "strings" |
| 7 | + "testing" |
| 8 | +) |
| 9 | + |
| 10 | +func TestIsJson(t *testing.T) { |
| 11 | + tests := []struct { |
| 12 | + name string |
| 13 | + reqContentType string |
| 14 | + reqBody string |
| 15 | + respContentType string |
| 16 | + respBody string |
| 17 | + maxContentLength int64 |
| 18 | + expectedResult bool |
| 19 | + }{ |
| 20 | + { |
| 21 | + name: "Valid JSON in both request and response with correct content types", |
| 22 | + reqContentType: "application/json", |
| 23 | + reqBody: `{"key": "value"}`, |
| 24 | + respContentType: "application/json", |
| 25 | + respBody: `{"key": "value"}`, |
| 26 | + maxContentLength: 1024, |
| 27 | + expectedResult: true, |
| 28 | + }, |
| 29 | + { |
| 30 | + name: "XML in request and response with correct Content-Type", |
| 31 | + reqContentType: "application/xml", |
| 32 | + reqBody: `<key>value</key>`, |
| 33 | + respContentType: "application/xml", |
| 34 | + respBody: `<key>value</key>`, |
| 35 | + maxContentLength: 1024, |
| 36 | + expectedResult: false, |
| 37 | + }, |
| 38 | + { |
| 39 | + name: "XML in request with JSON in response", |
| 40 | + reqContentType: "application/xml", |
| 41 | + reqBody: `<key>value</key>`, |
| 42 | + respContentType: "application/json", |
| 43 | + respBody: `{"key": "value"}`, |
| 44 | + maxContentLength: 1024, |
| 45 | + expectedResult: true, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "JSON in request with XML in response", |
| 49 | + reqContentType: "application/json", |
| 50 | + reqBody: `{"key": "value"}`, |
| 51 | + respContentType: "application/xml", |
| 52 | + respBody: `<key>value</key>`, |
| 53 | + maxContentLength: 1024, |
| 54 | + expectedResult: true, |
| 55 | + }, |
| 56 | + { |
| 57 | + name: "Empty request and response bodies and headers", |
| 58 | + reqContentType: "", |
| 59 | + reqBody: "", |
| 60 | + respContentType: "", |
| 61 | + respBody: "", |
| 62 | + maxContentLength: 1024, |
| 63 | + expectedResult: false, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "No content-type headers with valid JSON in request", |
| 67 | + reqContentType: "", |
| 68 | + reqBody: `{"key": "value"}`, |
| 69 | + respContentType: "", |
| 70 | + respBody: ``, |
| 71 | + maxContentLength: 1024, |
| 72 | + expectedResult: true, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "No content-type headers with valid JSON in response", |
| 76 | + reqContentType: "", |
| 77 | + reqBody: ``, |
| 78 | + respContentType: "", |
| 79 | + respBody: `{"key": "value"}`, |
| 80 | + maxContentLength: 1024, |
| 81 | + expectedResult: true, |
| 82 | + }, |
| 83 | + { |
| 84 | + name: "No content-type headers with invalid JSON in request", |
| 85 | + reqContentType: "", |
| 86 | + reqBody: `{"key": "value"`, |
| 87 | + respContentType: "", |
| 88 | + respBody: ``, |
| 89 | + maxContentLength: 1024, |
| 90 | + expectedResult: false, |
| 91 | + }, |
| 92 | + { |
| 93 | + name: "No content-type headers with invalid JSON in response", |
| 94 | + reqContentType: "", |
| 95 | + reqBody: ``, |
| 96 | + respContentType: "", |
| 97 | + respBody: `{"key": "value"`, |
| 98 | + maxContentLength: 1024, |
| 99 | + expectedResult: false, |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "Content-type geo+json in request with invalid body", |
| 103 | + reqContentType: "application/geo+json", |
| 104 | + reqBody: ``, |
| 105 | + respContentType: "", |
| 106 | + respBody: ``, |
| 107 | + maxContentLength: 1024, |
| 108 | + expectedResult: true, |
| 109 | + }, |
| 110 | + { |
| 111 | + name: "No content-type headers with request payload longer than max length", |
| 112 | + reqContentType: "", |
| 113 | + reqBody: `{"key": "` + strings.Repeat("a", 1025) + `"}`, |
| 114 | + respContentType: "", |
| 115 | + respBody: ``, |
| 116 | + maxContentLength: 1024, |
| 117 | + expectedResult: false, |
| 118 | + }, |
| 119 | + { |
| 120 | + name: "No content-type headers with response payload longer than max length", |
| 121 | + reqContentType: "", |
| 122 | + reqBody: ``, |
| 123 | + respContentType: "", |
| 124 | + respBody: `{"key": "` + strings.Repeat("a", 1025) + `"}`, |
| 125 | + maxContentLength: 1024, |
| 126 | + expectedResult: false, |
| 127 | + }, |
| 128 | + { |
| 129 | + name: "No content-type headers with request payload longer than max length and response payload shorter", |
| 130 | + reqContentType: "", |
| 131 | + reqBody: strings.Repeat("a", 1025), |
| 132 | + respContentType: "", |
| 133 | + respBody: `{"key": "value"}`, |
| 134 | + maxContentLength: 1024, |
| 135 | + expectedResult: true, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: "No content-type headers with request payload shorter than max length and response payload longer", |
| 139 | + reqContentType: "", |
| 140 | + reqBody: `{"key": "value"}`, |
| 141 | + respContentType: "", |
| 142 | + respBody: strings.Repeat("a", 1025), |
| 143 | + maxContentLength: 1024, |
| 144 | + expectedResult: true, |
| 145 | + }, |
| 146 | + } |
| 147 | + |
| 148 | + for _, tt := range tests { |
| 149 | + t.Run(tt.name, func(t *testing.T) { |
| 150 | + req, err := http.NewRequest("POST", "/", strings.NewReader(tt.reqBody)) |
| 151 | + if err != nil { |
| 152 | + t.Fatalf("Failed to create request: %v", err) |
| 153 | + } |
| 154 | + if tt.reqContentType != "" { |
| 155 | + req.Header.Set("Content-Type", tt.reqContentType) |
| 156 | + } |
| 157 | + |
| 158 | + resp := &http.Response{ |
| 159 | + Header: make(http.Header), |
| 160 | + Body: io.NopCloser(strings.NewReader(tt.respBody)), |
| 161 | + ContentLength: int64(len(tt.respBody)), |
| 162 | + } |
| 163 | + if tt.respContentType != "" { |
| 164 | + resp.Header.Set("Content-Type", tt.respContentType) |
| 165 | + } |
| 166 | + |
| 167 | + reqAndResp := httpRequestAndResponse{ |
| 168 | + request: req, |
| 169 | + response: resp, |
| 170 | + } |
| 171 | + |
| 172 | + result := isJson(&reqAndResp, tt.maxContentLength) |
| 173 | + if result != tt.expectedResult { |
| 174 | + t.Errorf("isJson() = %v, want %v", result, tt.expectedResult) |
| 175 | + } |
| 176 | + }) |
| 177 | + } |
| 178 | +} |
0 commit comments