Beispiel #1
0
func TestStaticFile(t *testing.T) {
	// prepare tempdir for static file root.
	rootDir, _ := ioutil.TempDir("", "staticfile")
	defer os.RemoveAll(rootDir)
	// bar.json
	ioutil.WriteFile(filepath.Join(rootDir, "test.txt"), []byte("Hello"), 0644)

	// Dummy request and response
	r, _ := http.NewRequest("GET", "http://example.com/s/notfound", nil)
	req := wcg.NewRequest(r)
	w := httptest.NewRecorder()
	res := wcg.NewResponse(w, req)
	StaticFile("/s", rootDir).Process(res, req)
	if res.StatusCode != 404 {
		t.Errorf("Static File for /s/notfound should return 404, but got %d", res.StatusCode)
	}

	r, _ = http.NewRequest("GET", "http://example.com/s/test.txt", nil)
	req = wcg.NewRequest(r)
	w = httptest.NewRecorder()
	res = wcg.NewResponse(w, req)
	StaticFile("/s", rootDir).Process(res, req)
	if res.StatusCode != 200 {
		t.Errorf("Static File for /s/test.txt should return 200, but got %d", res.StatusCode)
	}
	if w.Body.String() != "Hello" {
		t.Errorf("Static File for /s/test.txt contents should be 'Hello', but got %s", w.Body.String())
	}
}
func TestSessionSupport_Expire(t *testing.T) {
	assert := wcg.NewAssert(t)
	// default configugration
	cfg := &SessionConfig{
		StoreFactory: nil,
		Key:          "wcgsess",
		MaxAge:       1, // 3 seconds
		CookieName:   "wcgsess",
		Domain:       "",
		HTTPOnly:     true,
		Path:         "/",
	}
	load, save := SessionSupportWithConfig(cfg)
	r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
	req := wcg.NewRequest(r)
	w := httptest.NewRecorder()
	res := wcg.NewResponse(w, req)
	load.Process(res, req)
	req.Session.Set("foo", "bar")
	save.Process(res, req)
	sid1 := req.Session.ID

	time.Sleep(1 * time.Second)
	// Another request is coming with the same cookie.
	c := wcg.ParseCookie(w.Header().Get("set-cookie"))

	r, _ = http.NewRequest("GET", "http://example.com/foo", nil)
	r.Header.Set("cookie", c.String())
	req = wcg.NewRequest(r)
	w = httptest.NewRecorder()
	res = wcg.NewResponse(w, req)
	load.Process(res, req)
	sid2 := req.Session.ID
	assert.Not(sid1 == sid2, "Keep session")
}
func TestSessionSupport(t *testing.T) {
	assert := wcg.NewAssert(t)
	// default configugration
	cfg := SessionConfigIni
	load, save := SessionSupport()
	r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
	req := wcg.NewRequest(r)
	w := httptest.NewRecorder()
	res := wcg.NewResponse(w, req)
	load.Process(res, req)
	assert.NotNil(req.Session, "req.Session with SessionSupport")
	// application set the sessoin data.
	req.Session.Set("foo", "bar")

	// Check set-cookie header on response, which should include signed session id.
	c := wcg.ParseCookie(w.Header().Get("set-cookie"))
	assert.EqStr(cfg.CookieName, c.Name, "Issued cookie name")
	assert.NotZero(c.Value, "Issued cookie value")

	// Run save handler.
	save.Process(res, req)

	// Another request is coming with the same session id.
	r, _ = http.NewRequest("GET", "http://example.com/foo", nil)
	r.Header.Set("cookie", c.String())
	req = wcg.NewRequest(r)
	w = httptest.NewRecorder()
	res = wcg.NewResponse(w, req)
	load.Process(res, req)
	val, _ := req.Session.Get("foo")
	assert.EqStr("bar", val, "Stored value check")
}
Beispiel #4
0
// Run to run the server on GAE.
func (s *Server) Run() error {
	http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
		req := wcg.NewRequest(r)
		s.routes.Dispatch(wcg.NewResponse(w, req), req)
	})
	return nil
}
Beispiel #5
0
// Request to create a http request
func Request(method string, path string, body io.Reader) *TestRequest {
	r, err := http.NewRequest(method, path, body)
	if err != nil {
		panic(err)
	}
	req := wcg.NewTestRequest(r)
	recorder := httptest.NewRecorder()
	res := wcg.NewResponse(recorder, req)
	return &TestRequest{req, res, recorder}
}
Beispiel #6
0
func newTestRequestAndResponse() (*wcg.Response, *wcg.Request) {
	r, _ := http.NewRequest("GET", "http://example.com/foo", nil)
	r.RemoteAddr = "1.2.3.4:12345"
	r.Header.Set("User-Agent", "MyAgent")
	r.Header.Set("Referer", "http://example.com/bar")
	req := wcg.NewRequest(r)
	res := wcg.NewResponse(httptest.NewRecorder(), req)
	res.WriteString("Hello")
	res.StartTime = time.Date(2014, 1, 1, 12, 1, 5, 0, time.UTC)
	rt, _ := time.ParseDuration("3.1s")
	res.EndTime = res.StartTime.Add(rt)
	return res, req
}