-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
playground_test.go
73 lines (62 loc) · 2.27 KB
/
playground_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
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())
}
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())
}
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) {
rec := httptest.NewRecorder()
req := httptest.NewRequest(http.MethodGet, "http://localhost:8080/query", nil)
h := Handler("example.org API", "/customquery")
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)
}
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 {
panic(fmt.Errorf("reading res.Body: %w", err))
}
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())
}
}