@@ -1667,9 +1667,8 @@ func (f *localResponseWriter) WriteHeader(c int) {
16671667 f .statusCode = c
16681668}
16691669
1670- func TestwriteJSON (t * testing.T ) {
1671- testCode := 55
1672- testData , err := json .Marshal ("test data" )
1670+ func testWriteJSON (t * testing.T , testCode int , testData interface {}) {
1671+ testDataMarshalled , err := json .Marshal (testData )
16731672 if err != nil {
16741673 t .Fatal (err )
16751674 }
@@ -1679,10 +1678,17 @@ func TestwriteJSON(t *testing.T) {
16791678 if rsp .statusCode != testCode {
16801679 t .Fatalf ("writeJSON() failed to set the status code. Expected %d. Got %d" , testCode , rsp .statusCode )
16811680 }
1682- if ! bytes .Equal (testData , rsp .body ) {
1683- t .Fatalf ("writeJSON() failed to set the body. Expected %s. Got %s" , testData , rsp .body )
1681+ // writeJSON calls json.Encode and it appends '\n' to the result,
1682+ // while json.Marshal not
1683+ expected := append (testDataMarshalled , byte ('\n' ))
1684+ if ! bytes .Equal (expected , rsp .body ) {
1685+ t .Fatalf ("writeJSON() failed to set the body. Expected %q. Got %q" , expected , rsp .body )
16841686 }
1687+ }
16851688
1689+ func TestWriteJSON (t * testing.T ) {
1690+ testWriteJSON (t , 55 , "test data as string" )
1691+ testWriteJSON (t , 55 , []byte ("test data as bytes" ))
16861692}
16871693
16881694func TestHttpHandlerUninit (t * testing.T ) {
0 commit comments