github.com/senomas/gqlgen@v0.17.11-0.20220626120754-9aee61b0716a/graphql/playground/playground_test.go (about) 1 package playground 2 3 import ( 4 "fmt" 5 "io" 6 "net/http" 7 "net/http/httptest" 8 "regexp" 9 "testing" 10 ) 11 12 func TestHandler_createsAbsoluteURLs(t *testing.T) { 13 rec := httptest.NewRecorder() 14 req := httptest.NewRequest(http.MethodGet, "https://example.org/query", nil) 15 h := Handler("example.org API", "https://example.org/query") 16 h.ServeHTTP(rec, req) 17 18 res := rec.Result() 19 defer res.Body.Close() 20 if res.StatusCode != http.StatusOK { 21 t.Errorf("res.StatusCode = %d; want %d", res.StatusCode, http.StatusOK) 22 } 23 24 b, err := io.ReadAll(res.Body) 25 if err != nil { 26 panic(fmt.Errorf("reading res.Body: %w", err)) 27 } 28 29 want := regexp.MustCompile(`(?m)^.*url\s*=\s*['"]https:\/\/example\.org\/query["'].*$`) 30 if !want.Match(b) { 31 t.Errorf("no match for %s in response body", want.String()) 32 } 33 34 wantSubURL := regexp.MustCompile(`(?m)^.*subscriptionUrl\s*=\s*['"]wss:\/\/example\.org\/query["'].*$`) 35 if !wantSubURL.Match(b) { 36 t.Errorf("no match for %s in response body", wantSubURL.String()) 37 } 38 } 39 40 func TestHandler_createsRelativeURLs(t *testing.T) { 41 rec := httptest.NewRecorder() 42 req := httptest.NewRequest(http.MethodGet, "http://localhost:8080/query", nil) 43 h := Handler("example.org API", "/customquery") 44 h.ServeHTTP(rec, req) 45 46 res := rec.Result() 47 defer res.Body.Close() 48 if res.StatusCode != http.StatusOK { 49 t.Errorf("res.StatusCode = %d; want %d", res.StatusCode, http.StatusOK) 50 } 51 52 b, err := io.ReadAll(res.Body) 53 if err != nil { 54 panic(fmt.Errorf("reading res.Body: %w", err)) 55 } 56 57 wantURL := regexp.MustCompile(`(?m)^.*url\s*=\s*location\.protocol.*$`) 58 if !wantURL.Match(b) { 59 t.Errorf("no match for %s in response body", wantURL.String()) 60 } 61 wantSubURL := regexp.MustCompile(`(?m)^.*subscriptionUrl\s*=\s*wsProto.*['"]\/customquery['"].*$`) 62 if !wantSubURL.Match(b) { 63 t.Errorf("no match for %s in response body", wantSubURL.String()) 64 } 65 }