-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[tests] Add explicit testing of unmarshalled responses (#74)
Trying to debug why we're getting StatusCode of 0 for some events. To rule out a libhoney side issue, I wrote some tests to ensure unmarshal things correctly for both JSON and Msgpack. Didn't find any issues, but might as well commit the tests. These emulate the same structures used in shepherd when marshalling responses. Co-authored-by: Mike Goldsmth <[email protected]>
- Loading branch information
1 parent
b7f8afb
commit 5a185e0
Showing
1 changed file
with
89 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
package transmission | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"testing" | ||
|
||
"github.com/vmihailenco/msgpack" | ||
) | ||
|
||
// response struct sent from API | ||
type responseInBatch struct { | ||
ErrorStr string `json:"error,omitempty"` | ||
Status int `json:"status,omitempty"` | ||
} | ||
|
||
var ( | ||
srcBatchResponseSingle = []responseInBatch{ | ||
responseInBatch{ErrorStr: "something bad happened", Status: 500}, | ||
} | ||
srcBatchResponseMultiple = []responseInBatch{ | ||
responseInBatch{ErrorStr: "something bad happened", Status: 500}, | ||
responseInBatch{Status: 202}, | ||
} | ||
) | ||
|
||
func TestUnmarshalJSONResponse(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
encoder := json.NewEncoder(buf) | ||
encoder.SetEscapeHTML(false) | ||
err := encoder.Encode(srcBatchResponseMultiple) | ||
testOK(t, err) | ||
|
||
var responses []Response | ||
err = json.NewDecoder(buf).Decode(&responses) | ||
testOK(t, err) | ||
|
||
testEquals(t, responses[0].StatusCode, 500) | ||
testEquals(t, responses[0].Err.Error(), "something bad happened") | ||
testEquals(t, responses[1].StatusCode, 202) | ||
testEquals(t, responses[1].Err, nil) | ||
} | ||
|
||
func TestUnmarshalJSONResponseSingle(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
encoder := json.NewEncoder(buf) | ||
encoder.SetEscapeHTML(false) | ||
err := encoder.Encode(srcBatchResponseSingle) | ||
testOK(t, err) | ||
|
||
var responses []Response | ||
err = json.NewDecoder(buf).Decode(&responses) | ||
testOK(t, err) | ||
|
||
testEquals(t, responses[0].StatusCode, 500) | ||
testEquals(t, responses[0].Err.Error(), "something bad happened") | ||
} | ||
|
||
func TestUnmarshalMsgpackResponse(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
encoder := msgpack.NewEncoder(buf) | ||
encoder.UseJSONTag(true) | ||
err := encoder.Encode(srcBatchResponseMultiple) | ||
testOK(t, err) | ||
|
||
var responses []Response | ||
err = msgpack.NewDecoder(buf).Decode(&responses) | ||
testOK(t, err) | ||
|
||
testEquals(t, responses[0].StatusCode, 500) | ||
testEquals(t, responses[0].Err.Error(), "something bad happened") | ||
testEquals(t, responses[1].StatusCode, 202) | ||
testEquals(t, responses[1].Err, nil) | ||
} | ||
|
||
func TestUnmarshalMsgpackResponseSingle(t *testing.T) { | ||
buf := &bytes.Buffer{} | ||
encoder := msgpack.NewEncoder(buf) | ||
encoder.UseJSONTag(true) | ||
err := encoder.Encode(srcBatchResponseSingle) | ||
testOK(t, err) | ||
|
||
var responses []Response | ||
err = msgpack.NewDecoder(buf).Decode(&responses) | ||
testOK(t, err) | ||
|
||
testEquals(t, responses[0].StatusCode, 500) | ||
testEquals(t, responses[0].Err.Error(), "something bad happened") | ||
} |