Skip to content

Commit

Permalink
Move AssertMessageEqual to go1.7 buildflag file (#3050)
Browse files Browse the repository at this point in the history
  • Loading branch information
skmcgrail authored Dec 23, 2019
1 parent 9827fd9 commit 6c5afb8
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 52 deletions.
52 changes: 0 additions & 52 deletions private/protocol/eventstream/eventstreamtest/testing.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ package eventstreamtest
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net/http"
Expand Down Expand Up @@ -251,53 +249,3 @@ var EventExceptionTypeHeader = eventstream.Header{
Name: messageTypeHeader,
Value: eventstream.StringValue(exceptionMessageType),
}

// AssertMessageEqual compares to event stream messages, and determines if they
// are equal. Will trigger an testing Error if components of the message are
// not equal.
func AssertMessageEqual(t testing.TB, a, b eventstream.Message, msg ...interface{}) {
getHelper(t)()

ah, err := bytesEncodeHeader(a.Headers)
if err != nil {
t.Fatalf("unable to encode a's headers, %v", err)
}

bh, err := bytesEncodeHeader(b.Headers)
if err != nil {
t.Fatalf("unable to encode b's headers, %v", err)
}

if !bytes.Equal(ah, bh) {
aj, err := json.Marshal(ah)
if err != nil {
t.Fatalf("unable to json encode a's headers, %v", err)
}
bj, err := json.Marshal(bh)
if err != nil {
t.Fatalf("unable to json encode b's headers, %v", err)
}
t.Errorf("%s\nexpect headers: %v\n\t%v\nactual headers: %v\n\t%v\n",
fmt.Sprint(msg...),
base64.StdEncoding.EncodeToString(ah), aj,
base64.StdEncoding.EncodeToString(bh), bj,
)
}

if !bytes.Equal(a.Payload, b.Payload) {
t.Errorf("%s\nexpect payload: %v\nactual payload: %v\n",
fmt.Sprint(msg...),
base64.StdEncoding.EncodeToString(a.Payload),
base64.StdEncoding.EncodeToString(b.Payload),
)
}
}

func bytesEncodeHeader(v eventstream.Headers) ([]byte, error) {
var buf bytes.Buffer
if err := eventstream.EncodeHeaders(&buf, v); err != nil {
return nil, err
}

return buf.Bytes(), nil
}
63 changes: 63 additions & 0 deletions private/protocol/eventstream/eventstreamtest/testing_1_7.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// +build go1.7

package eventstreamtest

import (
"bytes"
"encoding/base64"
"encoding/json"
"fmt"
"testing"

"github.com/aws/aws-sdk-go/private/protocol/eventstream"
)

// AssertMessageEqual compares to event stream messages, and determines if they
// are equal. Will trigger an testing Error if components of the message are
// not equal.
func AssertMessageEqual(t testing.TB, a, b eventstream.Message, msg ...interface{}) {
getHelper(t)()

ah, err := bytesEncodeHeader(a.Headers)
if err != nil {
t.Fatalf("unable to encode a's headers, %v", err)
}

bh, err := bytesEncodeHeader(b.Headers)
if err != nil {
t.Fatalf("unable to encode b's headers, %v", err)
}

if !bytes.Equal(ah, bh) {
aj, err := json.Marshal(ah)
if err != nil {
t.Fatalf("unable to json encode a's headers, %v", err)
}
bj, err := json.Marshal(bh)
if err != nil {
t.Fatalf("unable to json encode b's headers, %v", err)
}
t.Errorf("%s\nexpect headers: %v\n\t%v\nactual headers: %v\n\t%v\n",
fmt.Sprint(msg...),
base64.StdEncoding.EncodeToString(ah), aj,
base64.StdEncoding.EncodeToString(bh), bj,
)
}

if !bytes.Equal(a.Payload, b.Payload) {
t.Errorf("%s\nexpect payload: %v\nactual payload: %v\n",
fmt.Sprint(msg...),
base64.StdEncoding.EncodeToString(a.Payload),
base64.StdEncoding.EncodeToString(b.Payload),
)
}
}

func bytesEncodeHeader(v eventstream.Headers) ([]byte, error) {
var buf bytes.Buffer
if err := eventstream.EncodeHeaders(&buf, v); err != nil {
return nil, err
}

return buf.Bytes(), nil
}

0 comments on commit 6c5afb8

Please sign in to comment.