@@ -103,20 +103,13 @@ func waitForSpans(t *testing.T, handler *mockZipkinHandler, expecting int) {
103
103
}
104
104
105
105
func TestThriftFormat (t * testing.T ) {
106
- server , handler := initializeTestServer (nil )
106
+ server , _ := initializeTestServer (nil )
107
107
defer server .Close ()
108
-
109
108
bodyBytes := zipkinSerialize ([]* zipkincore.Span {{}})
110
109
statusCode , resBodyStr , err := postBytes (server .URL + `/api/v1/spans` , bodyBytes , createHeader ("application/x-thrift" ))
111
110
assert .NoError (t , err )
112
111
assert .EqualValues (t , http .StatusAccepted , statusCode )
113
112
assert .EqualValues (t , "" , resBodyStr )
114
-
115
- handler .zipkinSpansHandler .(* mockZipkinHandler ).err = fmt .Errorf ("Bad times ahead" )
116
- statusCode , resBodyStr , err = postBytes (server .URL + `/api/v1/spans` , bodyBytes , createHeader ("application/x-thrift" ))
117
- assert .NoError (t , err )
118
- assert .EqualValues (t , http .StatusInternalServerError , statusCode )
119
- assert .EqualValues (t , "Cannot submit Zipkin batch: Bad times ahead\n " , resBodyStr )
120
113
}
121
114
122
115
func TestJsonFormat (t * testing.T ) {
@@ -229,9 +222,52 @@ func TestCannotReadBodyFromRequest(t *testing.T) {
229
222
req , err := http .NewRequest (http .MethodPost , "whatever" , & errReader {})
230
223
assert .NoError (t , err )
231
224
rw := dummyResponseWriter {}
232
- handler .saveSpans (& rw , req )
233
- assert .EqualValues (t , http .StatusInternalServerError , rw .myStatusCode )
234
- assert .EqualValues (t , "Unable to process request body: Simulated error reading body\n " , rw .myBody )
225
+
226
+ tests := []struct {
227
+ handler func (w http.ResponseWriter , r * http.Request )
228
+ }{
229
+ {handler : handler .saveSpans },
230
+ {handler : handler .saveSpansV2 },
231
+ }
232
+ for _ , test := range tests {
233
+ test .handler (& rw , req )
234
+ assert .EqualValues (t , http .StatusInternalServerError , rw .myStatusCode )
235
+ assert .EqualValues (t , "Unable to process request body: Simulated error reading body\n " , rw .myBody )
236
+ }
237
+ }
238
+
239
+ func TestSaveSpansV2 (t * testing.T ) {
240
+ server , handler := initializeTestServer (nil )
241
+ defer server .Close ()
242
+ tests := []struct {
243
+ body []byte
244
+ resBody string
245
+ code int
246
+ headers map [string ]string
247
+ }{
248
+ {body : []byte ("[]" ), code : http .StatusAccepted },
249
+ {body : gzipEncode ([]byte ("[]" )), code : http .StatusAccepted , headers : map [string ]string {"Content-Encoding" : "gzip" }},
250
+ {body : []byte ("[]" ), code : http .StatusBadRequest , headers : map [string ]string {"Content-Type" : "text/html" }, resBody : "Unsupported Content-Type\n " },
251
+ {body : []byte ("[]" ), code : http .StatusBadRequest , headers : map [string ]string {"Content-Encoding" : "gzip" }, resBody : "Unable to process request body: unexpected EOF\n " },
252
+ {body : []byte ("not good" ), code : http .StatusBadRequest , resBody : "Unable to process request body: invalid character 'o' in literal null (expecting 'u')\n " },
253
+ {body : []byte ("[{}]" ), code : http .StatusBadRequest , resBody : "Unable to process request body: validation failure list:\n id in body is required\n traceId in body is required\n " },
254
+ {body : []byte (`[{"id":"1111111111111111", "traceId":"1111111111111111", "localEndpoint": {"ipv4": "A"}}]` ), code : http .StatusBadRequest , resBody : "Unable to process request body: wrong ipv4\n " },
255
+ }
256
+ for _ , test := range tests {
257
+ h := createHeader ("application/json" )
258
+ for k , v := range test .headers {
259
+ h .Set (k , v )
260
+ }
261
+ statusCode , resBody , err := postBytes (server .URL + `/api/v2/spans` , test .body , h )
262
+ require .NoError (t , err )
263
+ assert .EqualValues (t , test .code , statusCode )
264
+ assert .EqualValues (t , test .resBody , resBody )
265
+ }
266
+ handler .zipkinSpansHandler .(* mockZipkinHandler ).err = fmt .Errorf ("Bad times ahead" )
267
+ statusCode , resBody , err := postBytes (server .URL + `/api/v2/spans` , []byte (`[{"id":"1111111111111111", "traceId":"1111111111111111"}]` ), createHeader ("application/json" ))
268
+ require .NoError (t , err )
269
+ assert .EqualValues (t , http .StatusInternalServerError , statusCode )
270
+ assert .EqualValues (t , "Cannot submit Zipkin batch: Bad times ahead\n " , resBody )
235
271
}
236
272
237
273
type errReader struct {}
0 commit comments