File: web_test.go

package info (click to toggle)
golang-github-igm-sockjs-go 3.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid, trixie
  • size: 512 kB
  • sloc: javascript: 39; makefile: 5
file content (132 lines) | stat: -rw-r--r-- 4,222 bytes parent folder | download | duplicates (2)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package sockjs

import (
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"
)

func TestXhrCors(t *testing.T) {
	recorder := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/", nil)
	xhrCors := xhrCorsFactory(Options{})
	xhrCors(recorder, req)
	acao := recorder.Header().Get("access-control-allow-origin")
	if acao != "*" {
		t.Errorf("Incorrect value for access-control-allow-origin header, got %s, expected %s", acao, "*")
	}
	req.Header.Set("origin", "localhost")
	xhrCors(recorder, req)
	acao = recorder.Header().Get("access-control-allow-origin")
	if acao != "localhost" {
		t.Errorf("Incorrect value for access-control-allow-origin header, got %s, expected %s", acao, "localhost")
	}
	req.Header.Set("access-control-request-headers", "some value")
	rec := httptest.NewRecorder()
	xhrCors(rec, req)
	if rec.Header().Get("access-control-allow-headers") != "some value" {
		t.Errorf("Incorent value for ACAH, got %s", rec.Header().Get("access-control-allow-headers"))
	}

	rec = httptest.NewRecorder()
	xhrCors(rec, req)
	if rec.Header().Get("access-control-allow-credentials") != "true" {
		t.Errorf("Incorent value for ACAC, got %s", rec.Header().Get("access-control-allow-credentials"))
	}

	// verify that if Access-Control-Allow-Credentials was previously set that xhrCors() does not duplicate the value
	rec = httptest.NewRecorder()
	rec.Header().Set("Access-Control-Allow-Credentials", "true")
	xhrCors(rec, req)
	acac := rec.Header()["Access-Control-Allow-Credentials"]
	if len(acac) != 1 || acac[0] != "true" {
		t.Errorf("Incorent value for ACAC, got %s", strings.Join(acac, ","))
	}
}

func TestCheckOriginCORSAllowedNullOrigin(t *testing.T) {
	recorder := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/", nil)
	xhrCors := xhrCorsFactory(Options{
		CheckOrigin: func(req *http.Request) bool {
			return true
		},
	})
	req.Header.Set("origin", "null")
	xhrCors(recorder, req)
	acao := recorder.Header().Get("access-control-allow-origin")
	if acao != "null" {
		t.Errorf("Incorrect value for access-control-allow-origin header, got %s, expected %s", acao, "null")
	}
}

func TestCheckOriginCORSAllowedEmptyOrigin(t *testing.T) {
	recorder := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/", nil)
	xhrCors := xhrCorsFactory(Options{
		CheckOrigin: func(req *http.Request) bool {
			return true
		},
	})
	xhrCors(recorder, req)
	acao := recorder.Header().Get("access-control-allow-origin")
	if acao != "*" {
		t.Errorf("Incorrect value for access-control-allow-origin header, got %s, expected %s", acao, "*")
	}
}

func TestCheckOriginCORSNotAllowed(t *testing.T) {
	recorder := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/", nil)
	xhrCors := xhrCorsFactory(Options{
		CheckOrigin: func(req *http.Request) bool {
			return false
		},
	})
	req.Header.Set("origin", "localhost")
	xhrCors(recorder, req)
	acao := recorder.Header().Get("access-control-allow-origin")
	if acao != "" {
		t.Errorf("Incorrect value for access-control-allow-origin header, got %s, expected %s", acao, "")
	}
}

func TestXhrOptions(t *testing.T) {
	rec := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "/", nil)
	xhrOptions(rec, req)
	if rec.Code != http.StatusNoContent {
		t.Errorf("Wrong response status code, expected %d, got %d", http.StatusNoContent, rec.Code)
	}
}

func TestCacheFor(t *testing.T) {
	rec := httptest.NewRecorder()
	cacheFor(rec, nil)
	cacheControl := rec.Header().Get("cache-control")
	if cacheControl != "public, max-age=31536000" {
		t.Errorf("Incorrect cache-control header value, got '%s'", cacheControl)
	}
	expires := rec.Header().Get("expires")
	if expires == "" {
		t.Errorf("Expires header should not be empty") // TODO(igm) check proper formating of string
	}
	maxAge := rec.Header().Get("access-control-max-age")
	if maxAge != "31536000" {
		t.Errorf("Incorrect value for access-control-max-age, got '%s'", maxAge)
	}
}

func TestNoCache(t *testing.T) {
	rec := httptest.NewRecorder()
	noCache(rec, nil)
}

func TestWelcomeHandler(t *testing.T) {
	rec := httptest.NewRecorder()
	welcomeHandler(rec, nil)
	if rec.Body.String() != "Welcome to SockJS!\n" {
		t.Errorf("Incorrect welcome message received, got '%s'", rec.Body.String())
	}
}