Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fix(transport): fix duplicate variables (#464)
  • Loading branch information
ykzts committed May 20, 2021
1 parent 76aaaef commit dd1f3d5
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/go.yml
Expand Up @@ -42,9 +42,10 @@ jobs:
- name: Test
env:
GO111MODULE: on
MANAEL_ENABLE_AVIF: true
run: |
mkdir -p cover
go test -race -coverprofile=coverage.txt -covermode=atomic
go test -race -coverprofile=coverage.txt -covermode=atomic -v
- uses: codecov/codecov-action@v1
with:
Expand Down
13 changes: 9 additions & 4 deletions transport.go
Expand Up @@ -72,14 +72,19 @@ func (t *Transport) makeRequest(r *http.Request) (*http.Request, error) {
return r2, nil
}

func scanAcceptHeader(r *http.Request, t string) string {
f := os.Getenv("MANAEL_ENABLE_AVIF")
func avifEnabled(w *http.Response) bool {
t := w.Header.Get("Content-Type")

return os.Getenv("MANAEL_ENABLE_AVIF") == "true" && t != "image/png"
}

func scanAcceptHeader(w *http.Response, r *http.Request) string {
a := r.Header.Get("Accept")

for _, v := range strings.Split(a, ",") {
t := strings.TrimSpace(v)

if f == "true" && t != "image/png" && strings.HasPrefix(t, "image/avif") {
if avifEnabled(w) && strings.HasPrefix(t, "image/avif") {
return "image/avif"
} else if strings.HasPrefix(t, "image/webp") {
return "image/webp"
Expand Down Expand Up @@ -112,7 +117,7 @@ func check(w *http.Response, r *http.Request) string {
return "*/*"
}

return scanAcceptHeader(r, t)
return scanAcceptHeader(w, r)
}

func convert(src io.Reader, t string) (*bytes.Buffer, error) {
Expand Down
59 changes: 59 additions & 0 deletions transport_test.go
Expand Up @@ -28,6 +28,7 @@ import (
"net"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"

Expand Down Expand Up @@ -452,3 +453,61 @@ func TestTransport_RoundTrip_xForwardedFor(t *testing.T) {
}
}
}

var transportTests7 = []struct {
accept string
path string
statusCode int
contentType string
}{
{
"image/avif,image/webp,image/*,*/*;q=0.8",
"/photo.jpeg",
http.StatusOK,
"image/avif",
},
{
"image/avif,image/webp,image/*,*/*;q=0.8",
"/logo.png",
http.StatusOK,
"image/webp",
},
}

func TestTransport_RoundTrip_avif(t *testing.T) {
if os.Getenv("MANAEL_ENABLE_AVIF") != "true" {
t.Skip("Skipping test when avif disabled.")
}

mux := http.NewServeMux()
mux.HandleFunc("/logo.png", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "testdata/logo.png")
})
mux.HandleFunc("/photo.jpeg", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "testdata/photo.jpeg")
})

ts := httptest.NewServer(mux)
defer ts.Close()

tr := &manael.Transport{ts.URL, http.DefaultTransport}

for _, tc := range transportTests7 {
req := httptest.NewRequest(http.MethodGet, "https://manael.test"+tc.path, nil)
req.Header.Set("Accept", tc.accept)

resp, err := tr.RoundTrip(req)
if err != nil {
t.Fatal(err)
}
defer resp.Body.Close()

if got, want := resp.StatusCode, tc.statusCode; got != want {
t.Errorf("Status Code is %d, want %d", got, want)
}

if got, want := resp.Header.Get("Content-Type"), tc.contentType; got != want {
t.Errorf("Content-Type is %s, want %s", got, want)
}
}
}

1 comment on commit dd1f3d5

@vercel
Copy link

@vercel vercel bot commented on dd1f3d5 May 20, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.