Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(transport): fix duplicate variables #464

Merged
merged 1 commit into from May 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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)
}
}
}