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

     1  package sockjs
     2  
     3  import (
     4  	"net/http"
     5  	"net/http/httptest"
     6  	"strings"
     7  	"testing"
     8  )
     9  
    10  func TestHandler_htmlFileNoCallback(t *testing.T) {
    11  	h := newTestHandler()
    12  	rw := httptest.NewRecorder()
    13  	req, _ := http.NewRequest("GET", "/server/session/htmlfile", nil)
    14  	h.htmlFile(rw, req)
    15  	if rw.Code != http.StatusInternalServerError {
    16  		t.Errorf("Unexpected response code, got '%d', expected '%d'", rw.Code, http.StatusInternalServerError)
    17  	}
    18  	expectedContentType := "text/plain; charset=utf-8"
    19  	if rw.Header().Get("content-type") != expectedContentType {
    20  		t.Errorf("Unexpected content type, got '%s', expected '%s'", rw.Header().Get("content-type"), expectedContentType)
    21  	}
    22  }
    23  
    24  func TestHandler_htmlFile(t *testing.T) {
    25  	h := newTestHandler()
    26  	rw := httptest.NewRecorder()
    27  	req, _ := http.NewRequest("GET", "/server/session/htmlfile?c=testCallback", nil)
    28  	h.htmlFile(rw, req)
    29  	if rw.Code != http.StatusOK {
    30  		t.Errorf("Unexpected response code, got '%d', expected '%d'", rw.Code, http.StatusOK)
    31  	}
    32  	expectedContentType := "text/html; charset=UTF-8"
    33  	if rw.Header().Get("content-type") != expectedContentType {
    34  		t.Errorf("Unexpected content-type, got '%s', expected '%s'", rw.Header().Get("content-type"), expectedContentType)
    35  	}
    36  	if rw.Body.String() != expectedIFrame {
    37  		t.Errorf("Unexpected response body, got '%s', expected '%s'", rw.Body, expectedIFrame)
    38  	}
    39  
    40  }
    41  
    42  func init() {
    43  	expectedIFrame += strings.Repeat(" ", 1024-len(expectedIFrame)+len("testCallack")+13)
    44  	expectedIFrame += "\r\n\r\n"
    45  	expectedIFrame += "<script>\np(\"o\");\n</script>\r\n"
    46  }
    47  
    48  var expectedIFrame = `<!doctype html>
    49  <html><head>
    50    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    51    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    52  </head><body><h2>Don't panic!</h2>
    53    <script>
    54      document.domain = document.domain;
    55      var c = parent.testCallback;
    56      c.start();
    57      function p(d) {c.message(d);};
    58      window.onload = function() {c.stop();};
    59    </script>
    60  `