github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/igm/sockjs-go.v2/sockjs/options_test.go (about) 1 package sockjs 2 3 import ( 4 "encoding/json" 5 "net/http" 6 "net/http/httptest" 7 ) 8 import "testing" 9 10 func TestInfoGet(t *testing.T) { 11 recorder := httptest.NewRecorder() 12 request, _ := http.NewRequest("GET", "", nil) 13 DefaultOptions.info(recorder, request) 14 15 if recorder.Code != http.StatusOK { 16 t.Errorf("Wrong status code, got '%d' expected '%d'", recorder.Code, http.StatusOK) 17 } 18 19 decoder := json.NewDecoder(recorder.Body) 20 var a info 21 decoder.Decode(&a) 22 if !a.Websocket { 23 t.Errorf("Websocket field should be set true") 24 } 25 if a.CookieNeeded { 26 t.Errorf("CookieNeede should be set to false") 27 } 28 } 29 30 func TestInfoOptions(t *testing.T) { 31 recorder := httptest.NewRecorder() 32 request, _ := http.NewRequest("OPTIONS", "", nil) 33 DefaultOptions.info(recorder, request) 34 if recorder.Code != http.StatusNoContent { 35 t.Errorf("Incorrect status code received, got '%d' expected '%d'", recorder.Code, http.StatusNoContent) 36 } 37 } 38 39 func TestInfoUnknown(t *testing.T) { 40 req, _ := http.NewRequest("PUT", "", nil) 41 rec := httptest.NewRecorder() 42 DefaultOptions.info(rec, req) 43 if rec.Code != http.StatusNotFound { 44 t.Errorf("Incorrec response status, got '%d' expected '%d'", rec.Code, http.StatusNotFound) 45 } 46 } 47 48 func TestCookies(t *testing.T) { 49 rec := httptest.NewRecorder() 50 req, _ := http.NewRequest("GET", "", nil) 51 optionsWithCookies := DefaultOptions 52 optionsWithCookies.JSessionID = DefaultJSessionID 53 optionsWithCookies.cookie(rec, req) 54 if rec.Header().Get("set-cookie") != "JSESSIONID=dummy; Path=/" { 55 t.Errorf("Cookie not properly set in response") 56 } 57 // cookie value set in request 58 req.AddCookie(&http.Cookie{Name: "JSESSIONID", Value: "some_jsession_id", Path: "/"}) 59 rec = httptest.NewRecorder() 60 optionsWithCookies.cookie(rec, req) 61 if rec.Header().Get("set-cookie") != "JSESSIONID=some_jsession_id; Path=/" { 62 t.Errorf("Cookie not properly set in response") 63 } 64 }