Skip to content

Commit

Permalink
[tests] Add explicit testing of unmarshalled responses (#74)
Browse files Browse the repository at this point in the history
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
tredman and MikeGoldsmith authored Jun 21, 2021
1 parent b7f8afb commit 5a185e0
Showing 1 changed file with 89 additions and 0 deletions.
89 changes: 89 additions & 0 deletions transmission/response_test.go
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")
}

0 comments on commit 5a185e0

Please sign in to comment.