Skip to content

Commit

Permalink
Fix invalid query parameter for playground subscription endpoint (#2148)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcusirgens authored May 5, 2022
1 parent fb5751a commit 0f016df
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
2 changes: 1 addition & 1 deletion graphql/playground/playground.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var page = template.Must(template.New("graphiql").Parse(`<!DOCTYPE html>
{{- else}}
const url = location.protocol + '//' + location.host + {{.endpoint}};
const wsProto = location.protocol == 'https:' ? 'wss:' : 'ws:';
const subscriptionUrl = wsProto + '//' + location.host + '/foo';
const subscriptionUrl = wsProto + '//' + location.host + {{.endpoint}};
{{- end}}
const fetcher = GraphiQL.createFetcher({ url, subscriptionUrl });
Expand Down
17 changes: 13 additions & 4 deletions graphql/playground/playground_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,17 @@ func TestHandler_createsAbsoluteURLs(t *testing.T) {
if !want.Match(b) {
t.Errorf("no match for %s in response body", want.String())
}

wantSubURL := regexp.MustCompile(`(?m)^.*subscriptionUrl\s*=\s*['"]wss:\/\/example\.org\/query["'].*$`)
if !wantSubURL.Match(b) {
t.Errorf("no match for %s in response body", wantSubURL.String())
}
}

func TestHandler_createsRelativeURLs(t *testing.T) {
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "http://localhost:8080/query", nil)
h := Handler("example.org API", "/query")
h := Handler("example.org API", "/customquery")
h.ServeHTTP(rec, req)

res := rec.Result()
Expand All @@ -49,8 +54,12 @@ func TestHandler_createsRelativeURLs(t *testing.T) {
panic(fmt.Errorf("reading res.Body: %w", err))
}

want := regexp.MustCompile(`(?m)^.*url\s*=\s*location.protocol.*$`)
if !want.Match(b) {
t.Errorf("no match for %s in response body", want.String())
wantURL := regexp.MustCompile(`(?m)^.*url\s*=\s*location\.protocol.*$`)
if !wantURL.Match(b) {
t.Errorf("no match for %s in response body", wantURL.String())
}
wantSubURL := regexp.MustCompile(`(?m)^.*subscriptionUrl\s*=\s*wsProto.*['"]\/customquery['"].*$`)
if !wantSubURL.Match(b) {
t.Errorf("no match for %s in response body", wantSubURL.String())
}
}

0 comments on commit 0f016df

Please sign in to comment.