Skip to content

Instantly share code, notes, and snippets.

@yanmendes
Created May 8, 2019 20:34
Show Gist options
  • Save yanmendes/47f1aa1515599b8bd1cb1b88db47b48b to your computer and use it in GitHub Desktop.
Save yanmendes/47f1aa1515599b8bd1cb1b88db47b48b to your computer and use it in GitHub Desktop.
package proxy
import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"
. "github.com/smartystreets/goconvey/convey"
)
func TestDocumentTransport(t *testing.T) {
Convey("RoundTrip", t, func() {
Convey("transforms returned body", func() {
t := &documentTransport{
transport: roundTripReturningImage(http.StatusOK, `"image":{"url":"http://my-fake-image.jpeg"}`),
transformBody: replaceImagesURLProtocol(),
}
// Act
res, err := t.RoundTrip(httptest.NewRequest("GET", "http://target.com", nil))
// Assert
So(err, ShouldBeNil)
body, _ := ioutil.ReadAll(res.Body)
So(string(body), ShouldEqual, `"image":{"url":"https://my-fake-image.jpeg"}`)
})
})
}
func roundTripReturningImage(statusCode int, body string) http.RoundTripper {
return &fakeRoundTripper{
roundTrip: func(req *http.Request) (*http.Response, error) {
return &http.Response{
Header: http.Header{},
StatusCode: statusCode,
Body: newBody(body),
}, nil
},
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment