Created
May 8, 2019 20:34
-
-
Save yanmendes/47f1aa1515599b8bd1cb1b88db47b48b to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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