Skip to content

Commit

Permalink
feat: make Playground HTML content compatible with UTF-8 charset (#2355)
Browse files Browse the repository at this point in the history
  • Loading branch information
nekomeowww committed Sep 6, 2022
1 parent 182b039 commit 56574a1
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
3 changes: 2 additions & 1 deletion graphql/playground/playground.go
Expand Up @@ -9,6 +9,7 @@ import (
var page = template.Must(template.New("graphiql").Parse(`<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{.title}}</title>
<style>
body {
Expand Down Expand Up @@ -75,7 +76,7 @@ var page = template.Must(template.New("graphiql").Parse(`<!DOCTYPE html>
// Handler responsible for setting up the playground
func Handler(title string, endpoint string) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
w.Header().Add("Content-Type", "text/html")
w.Header().Add("Content-Type", "text/html; charset=UTF-8")
err := page.Execute(w, map[string]interface{}{
"title": title,
"endpoint": endpoint,
Expand Down
8 changes: 8 additions & 0 deletions graphql/playground/playground_test.go
Expand Up @@ -35,6 +35,11 @@ func TestHandler_createsAbsoluteURLs(t *testing.T) {
if !wantSubURL.Match(b) {
t.Errorf("no match for %s in response body", wantSubURL.String())
}

wantMetaCharsetElement := regexp.MustCompile(`<head>\n\s{0,}<meta charset="utf-8">\n\s{0,}.*<title>`) // <meta> element must be in <head> and before <title>
if !wantMetaCharsetElement.Match(b) {
t.Errorf("no match for %s in response body", wantMetaCharsetElement.String())
}
}

func TestHandler_createsRelativeURLs(t *testing.T) {
Expand All @@ -48,6 +53,9 @@ func TestHandler_createsRelativeURLs(t *testing.T) {
if res.StatusCode != http.StatusOK {
t.Errorf("res.StatusCode = %d; want %d", res.StatusCode, http.StatusOK)
}
if res.Header.Get("Content-Type") != "text/html; charset=UTF-8" {
t.Errorf("res.Header.Get(\"Content-Type\") = %q; want %q", res.Header.Get("Content-Type"), "text/html; charset=UTF-8")
}

b, err := io.ReadAll(res.Body)
if err != nil {
Expand Down

0 comments on commit 56574a1

Please sign in to comment.