Skip to content

Commit

Permalink
go: Add basic test for bundle.go (#417)
Browse files Browse the repository at this point in the history
  • Loading branch information
irori authored Mar 29, 2019
1 parent ba2b3b6 commit 487734b
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions go/bundle/bundle_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package bundle_test

import (
"bytes"
"net/http"
"net/url"
"reflect"
"testing"

. "github.com/WICG/webpackage/go/bundle"
)

func urlMustParse(rawurl string) *url.URL {
u, err := url.Parse(rawurl)
if err != nil {
panic(err)
}
return u
}

func createTestBundle() *Bundle {
return &Bundle{
Exchanges: []*Exchange{
&Exchange{
Request{
URL: urlMustParse("https://bundle.example.com/"),
Header: make(http.Header),
},
Response{
Status: 200,
Header: http.Header{"Content-Type": []string{"text/html"}},
Body: []byte("hello, world!"),
},
},
},
}
}

func TestWriteAndRead(t *testing.T) {
bundle := createTestBundle()

var buf bytes.Buffer
n, err := bundle.WriteTo(&buf)
if err != nil {
t.Errorf("Bundle.WriteTo unexpectedly failed: %v", err)
}
if n != int64(buf.Len()) {
t.Errorf("Bundle.WriteTo returned %d, but wrote %d bytes", n, buf.Len())
}

deserialized, err := Read(&buf)
if err != nil {
t.Errorf("Bundle.Read unexpectedly failed: %v", err)
}
if !reflect.DeepEqual(deserialized, bundle) {
t.Errorf("got: %v\nwant: %v", deserialized, bundle)
}
}

0 comments on commit 487734b

Please sign in to comment.