github.com/insionng/yougam@v0.0.0-20170714101924-2bc18d833463/libraries/igm/sockjs-go.v2/sockjs/web_test.go (about)

     1  package sockjs
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"testing"
     7  )
     8  
     9  func TestXhrCors(t *testing.T) {
    10  	recorder := httptest.NewRecorder()
    11  	req, _ := http.NewRequest("GET", "/", nil)
    12  	xhrCors(recorder, req)
    13  	acao := recorder.Header().Get("access-control-allow-origin")
    14  	if acao != "*" {
    15  		t.Errorf("Incorrect value for access-control-allow-origin header, got %s, expected %s", acao, "*")
    16  	}
    17  	req.Header.Set("origin", "localhost")
    18  	xhrCors(recorder, req)
    19  	acao = recorder.Header().Get("access-control-allow-origin")
    20  	if acao != "localhost" {
    21  		t.Errorf("Incorrect value for access-control-allow-origin header, got %s, expected %s", acao, "localhost")
    22  	}
    23  
    24  	req.Header.Set("access-control-request-headers", "some value")
    25  	rec := httptest.NewRecorder()
    26  	xhrCors(rec, req)
    27  	if rec.Header().Get("access-control-allow-headers") != "some value" {
    28  		t.Errorf("Incorent value for ACAH, got %s", rec.Header().Get("access-control-allow-headers"))
    29  	}
    30  }
    31  
    32  func TestXhrOptions(t *testing.T) {
    33  	rec := httptest.NewRecorder()
    34  	req, _ := http.NewRequest("GET", "/", nil)
    35  	xhrOptions(rec, req)
    36  	if rec.Code != http.StatusNoContent {
    37  		t.Errorf("Wrong response status code, expected %d, got %d", http.StatusNoContent, rec.Code)
    38  	}
    39  }
    40  
    41  func TestCacheFor(t *testing.T) {
    42  	rec := httptest.NewRecorder()
    43  	cacheFor(rec, nil)
    44  	cacheControl := rec.Header().Get("cache-control")
    45  	if cacheControl != "public, max-age=31536000" {
    46  		t.Errorf("Incorrect cache-control header value, got '%s'", cacheControl)
    47  	}
    48  	expires := rec.Header().Get("expires")
    49  	if expires == "" {
    50  		t.Errorf("Expires header should not be empty") // TODO(igm) check proper formating of string
    51  	}
    52  	maxAge := rec.Header().Get("access-control-max-age")
    53  	if maxAge != "31536000" {
    54  		t.Errorf("Incorrect value for access-control-max-age, got '%s'", maxAge)
    55  	}
    56  }
    57  
    58  func TestNoCache(t *testing.T) {
    59  	rec := httptest.NewRecorder()
    60  	noCache(rec, nil)
    61  }
    62  
    63  func TestWelcomeHandler(t *testing.T) {
    64  	rec := httptest.NewRecorder()
    65  	welcomeHandler(rec, nil)
    66  	if rec.Body.String() != "Welcome to SockJS!\n" {
    67  		t.Errorf("Incorrect welcome message received, got '%s'", rec.Body.String())
    68  	}
    69  }