-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow absolute URLs to the GraphQL playground (#2142)
* Allow absolute URLs to the GraphQL playground * Add test for playground URLs * Close res.Body in playground test
- Loading branch information
1 parent
3228f36
commit d38911f
Showing
2 changed files
with
98 additions
and
10 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
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,56 @@ | ||
package playground | ||
|
||
import ( | ||
"fmt" | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"regexp" | ||
"testing" | ||
) | ||
|
||
func TestHandler_createsAbsoluteURLs(t *testing.T) { | ||
rec := httptest.NewRecorder() | ||
req := httptest.NewRequest(http.MethodGet, "https://example.org/query", nil) | ||
h := Handler("example.org API", "https://example.org/query") | ||
h.ServeHTTP(rec, req) | ||
|
||
res := rec.Result() | ||
defer res.Body.Close() | ||
if res.StatusCode != http.StatusOK { | ||
t.Errorf("res.StatusCode = %d; want %d", res.StatusCode, http.StatusOK) | ||
} | ||
|
||
b, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
panic(fmt.Errorf("reading res.Body: %w", err)) | ||
} | ||
|
||
want := regexp.MustCompile(`(?m)^.*url\s*=\s*['"]https:\/\/example\.org\/query["'].*$`) | ||
if !want.Match(b) { | ||
t.Errorf("no match for %s in response body", want.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.ServeHTTP(rec, req) | ||
|
||
res := rec.Result() | ||
defer res.Body.Close() | ||
if res.StatusCode != http.StatusOK { | ||
t.Errorf("res.StatusCode = %d; want %d", res.StatusCode, http.StatusOK) | ||
} | ||
|
||
b, err := io.ReadAll(res.Body) | ||
if err != nil { | ||
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()) | ||
} | ||
} |