This repository has been archived by the owner on Jun 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from escaletech/feature/serve-images-via-https
Override default transport for document requests
- Loading branch information
Showing
4 changed files
with
93 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package proxy | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
"io/ioutil" | ||
"net/http" | ||
"regexp" | ||
) | ||
|
||
type bodyTransformation func(body io.ReadCloser, req *http.Request) (io.ReadCloser, error) | ||
|
||
func newDocumentTransport(inner http.RoundTripper) *documentTransport { | ||
return &documentTransport{inner, replaceImagesURLProtocol()} | ||
} | ||
|
||
type documentTransport struct { | ||
transport http.RoundTripper | ||
transformBody bodyTransformation | ||
} | ||
|
||
func (t *documentTransport) RoundTrip(req *http.Request) (*http.Response, error) { | ||
res, err := t.transport.RoundTrip(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := t.transformBody(res.Body, req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
res.Body = body | ||
|
||
return res, nil | ||
} | ||
|
||
var regex = regexp.MustCompile(`(?m)"url":"http(:[^"]+)"`) | ||
|
||
func replaceImagesURLProtocol() bodyTransformation { | ||
return func(body io.ReadCloser, req *http.Request) (io.ReadCloser, error) { | ||
content, err := ioutil.ReadAll(body) | ||
if err != nil { | ||
return nil, err | ||
} | ||
fixed := regex.ReplaceAllString(string(content), `"url":"https$1"`) | ||
|
||
return ioutil.NopCloser(bytes.NewReader([]byte(fixed))), nil | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
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: roundTripReturningBody(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 roundTripReturningBody(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 | ||
}, | ||
} | ||
} |
This file contains 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
This file contains 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