Skip to content

Commit

Permalink
test: make envelope generation easier to test
Browse files Browse the repository at this point in the history
  • Loading branch information
AbhiPrasad committed Jun 3, 2020
1 parent e85b895 commit 0c673e5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 30 deletions.
6 changes: 3 additions & 3 deletions transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ func getRequestBodyFromEvent(event *Event) []byte {
return nil
}

func getEnvelopeFromBody(body []byte) *bytes.Buffer {
func getEnvelopeFromBody(body []byte, now time.Time) *bytes.Buffer {
var b bytes.Buffer
fmt.Fprintf(&b, `{"sent_at":"%s"}`, time.Now().UTC().Format(time.RFC3339Nano))
fmt.Fprintf(&b, `{"sent_at":"%s"}`, now.UTC().Format(time.RFC3339Nano))
fmt.Fprint(&b, "\n", `{"type":"transaction"}`, "\n")
b.Write(body)
b.WriteString("\n")
Expand All @@ -110,7 +110,7 @@ func getRequestFromEvent(event *Event, dsn *Dsn) (*http.Request, error) {
}

if event.Type == transactionType {
env := getEnvelopeFromBody(body)
env := getEnvelopeFromBody(body, time.Now())
request, _ := http.NewRequest(
http.MethodPost,
dsn.EnvelopeAPIURL().String(),
Expand Down
34 changes: 7 additions & 27 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ const enhancedEvent = "{\"extra\":{\"info\":\"Original event couldn't be marshal
"the data that uses interface{} type. Please verify that the data you attach to the scope is serializable.\"}," +
"\"message\":\"mkey\",\"sdk\":{},\"user\":{}}"

const envPayload = `{"type":"transaction"}` +
`{"type":"transaction","sdk":{},"timestamp":"1970-01-01T00:00:05Z","user":{},"start_timestamp":"1970-01-01T00:00:03Z"}`

func TestGetRequestBodyFromEventValid(t *testing.T) {
body := getRequestBodyFromEvent(&Event{
Message: "mkey",
Expand Down Expand Up @@ -138,30 +135,13 @@ func TestGetEnvelopeFromBody(t *testing.T) {
StartTimestamp: time.Unix(3, 0).UTC(),
Timestamp: time.Unix(5, 0).UTC(),
})
env := getEnvelopeFromBody(body)
envString := env.String()
envParts := strings.Split(envString, "\n")

if len(envParts) != 4 {
t.Errorf("\"%s\" should have len of 4, instead has len of %d", envParts, len(envParts))
}

if envParts[3] != "" {
t.Errorf("\"%s\" is not empty", envParts[3])
}

var header map[string]interface{}
err := json.Unmarshal([]byte(envParts[0]), &header)
if err != nil {
t.Fatal(err)
}
sentAtStr := header["sent_at"].(string)
if sentAtStr == "" {
t.Error("sent_at was not set in header")
}

want := envParts[1] + envParts[2]
got := envPayload
env := getEnvelopeFromBody(body, time.Unix(6, 0))
want := env.String()
got := strings.Join([]string{
"{\"sent_at\":\"1970-01-01T00:00:06Z\"}\n",
"{\"type\":\"transaction\"}\n",
"{\"type\":\"transaction\",\"sdk\":{},\"timestamp\":\"1970-01-01T00:00:05Z\",\"user\":{},\"start_timestamp\":\"1970-01-01T00:00:03Z\"}\n",
}, "")
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("Event mismatch (-want +got):\n%s", diff)
}
Expand Down

0 comments on commit 0c673e5

Please sign in to comment.