File: options_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 (64 lines) | stat: -rw-r--r-- 1,874 bytes parent folder | download
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
package sockjs

import (
	"encoding/json"
	"net/http"
	"net/http/httptest"
)
import "testing"

func TestInfoGet(t *testing.T) {
	recorder := httptest.NewRecorder()
	request, _ := http.NewRequest("GET", "", nil)
	DefaultOptions.info(recorder, request)

	if recorder.Code != http.StatusOK {
		t.Errorf("Wrong status code, got '%d' expected '%d'", recorder.Code, http.StatusOK)
	}

	decoder := json.NewDecoder(recorder.Body)
	var a info
	decoder.Decode(&a)
	if !a.Websocket {
		t.Errorf("Websocket field should be set true")
	}
	if a.CookieNeeded {
		t.Errorf("CookieNeeded should be set to false")
	}
}

func TestInfoOptions(t *testing.T) {
	recorder := httptest.NewRecorder()
	request, _ := http.NewRequest("OPTIONS", "", nil)
	DefaultOptions.info(recorder, request)
	if recorder.Code != http.StatusNoContent {
		t.Errorf("Incorrect status code received, got '%d' expected '%d'", recorder.Code, http.StatusNoContent)
	}
}

func TestInfoUnknown(t *testing.T) {
	req, _ := http.NewRequest("PUT", "", nil)
	rec := httptest.NewRecorder()
	DefaultOptions.info(rec, req)
	if rec.Code != http.StatusNotFound {
		t.Errorf("Incorrec response status, got '%d' expected '%d'", rec.Code, http.StatusNotFound)
	}
}

func TestCookies(t *testing.T) {
	rec := httptest.NewRecorder()
	req, _ := http.NewRequest("GET", "", nil)
	optionsWithCookies := DefaultOptions
	optionsWithCookies.JSessionID = DefaultJSessionID
	optionsWithCookies.cookie(rec, req)
	if rec.Header().Get("set-cookie") != "JSESSIONID=dummy; Path=/" {
		t.Errorf("Cookie not properly set in response")
	}
	// cookie value set in request
	req.AddCookie(&http.Cookie{Name: "JSESSIONID", Value: "some_jsession_id", Path: "/"})
	rec = httptest.NewRecorder()
	optionsWithCookies.cookie(rec, req)
	if rec.Header().Get("set-cookie") != "JSESSIONID=some_jsession_id; Path=/" {
		t.Errorf("Cookie not properly set in response")
	}
}