Пример #1
0
func TestHandlerAppendSlash(t *testing.T) {
	oldAppendSetting := Settings.Bool("append_slash", false)
	oldAppendRedirectSetting := Settings.Bool("append_slash_should_redirect", true)

	Settings.Set("append_slash", false)
	Pattern("/hello", &AppendSlashBaseHandler{})
	r, _ := http.NewRequest("GET", "/hello/", nil)
	rec := httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusNotFound, rec.Code)
	Mux = &PatternServeMux{}

	Settings.Set("append_slash", true)
	Pattern("/hello", &AppendSlashBaseHandler{})
	r, _ = http.NewRequest("GET", "/hello/", nil)
	rec = httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusOK, rec.Code)
	Mux = &PatternServeMux{}

	Settings.Set("append_slash", true)
	Settings.Set("append_slash_should_redirect", true)
	Pattern("/hello", &AppendSlashBaseHandler{})
	r, _ = http.NewRequest("GET", "/hello/", nil)
	rec = httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusMovedPermanently, rec.Code)
	Mux = &PatternServeMux{}

	// Set it back to what it was.
	Settings.Set("append_slash", oldAppendSetting)
	Settings.Set("append_slash_should_redirect", oldAppendRedirectSetting)
}
Пример #2
0
func TestResponseNewSlice(t *testing.T) {
	resp := NewHttpResponse([]string{"hellowww", "aaaaa"})

	assert.Equal(t, `["hellowww","aaaaa"]`, resp.Content)
	assert.Equal(t, 200, resp.StatusCode)
	assert.Equal(t, "application/json; charset=utf-8", resp.ContentType)
}
Пример #3
0
func TestResponseNewMap(t *testing.T) {
	v := map[string]interface{}{"k": "value"}
	resp := NewHttpResponse(v)

	assert.Equal(t, `{"k":"value"}`, resp.Content)
	assert.Equal(t, 200, resp.StatusCode)
	assert.Equal(t, "application/json; charset=utf-8", resp.ContentType)
}
Пример #4
0
func TestResponseNewNoParams(t *testing.T) {
	resp := NewHttpResponse()

	assert.Equal(t, "", resp.Content)
	assert.Equal(t, 200, resp.StatusCode)
	assert.Equal(t, "text/html; charset=utf-8", resp.ContentType)
	assert.Equal(t, 0, len(resp.Header))
	assert.Equal(t, 0, len(resp.Context))
}
Пример #5
0
func TestResponseNewThreeParams(t *testing.T) {
	resp := NewHttpResponse("My Content", 202, "text/plain; charset=latin1")

	assert.Equal(t, "My Content", resp.Content)
	assert.Equal(t, 202, resp.StatusCode)
	assert.Equal(t, "text/plain; charset=latin1", resp.ContentType)
	assert.Equal(t, 0, len(resp.Header))
	assert.Equal(t, 0, len(resp.Context))
}
Пример #6
0
func TestRequestFragementValue(t *testing.T) {
	in, _ := http.NewRequest("GET", "example.com?foo=bar#batman", nil)
	req := NewHttpRequest(in, make(url.Values))
	assert.Equal(t, "batman", req.FragmentValue())

	in, _ = http.NewRequest("GET", "example.com?foo=bar", nil)
	req = NewHttpRequest(in, make(url.Values))
	assert.Equal(t, "", req.FragmentValue())
}
Пример #7
0
func TestDictObjExists(t *testing.T) {
	dictObj := NewDictObj()

	dictObj.Set("a", "AAA")
	dictObj.Set("b", "BBB")

	assert.Equal(t, true, dictObj.Exists("a"))
	assert.Equal(t, true, dictObj.Exists("b"))
	assert.Equal(t, false, dictObj.Exists("c"))
}
Пример #8
0
func TestRequestNew(t *testing.T) {
	b := bytes.NewBufferString("sample body here")
	in, _ := http.NewRequest("GET", "http://example.com", b)
	req := NewHttpRequest(in, make(url.Values))

	assert.Equal(t, "sample body here", req.BodyString)
	assert.Equal(t, 0, len(req.PathArgs))
	assert.Equal(t, 0, len(req.Registry))
	assert.Equal(t, "example.com", req.URL.Host)
	assert.Equal(t, "GET", req.Method)
}
Пример #9
0
func TestServerVersionMap(t *testing.T) {
	oldVer := Version
	Version = "2.5.11"

	vmap := VersionMap()
	assert.Equal(t, 2, vmap[0])
	assert.Equal(t, 5, vmap[1])
	assert.Equal(t, 11, vmap[2])

	Version = oldVer
}
Пример #10
0
func TestHandlerPrepareFinish(t *testing.T) {
	defer func() { Mux = &PatternServeMux{} }()
	Pattern("/", &PrepFinHandler{})

	r, _ := http.NewRequest("GET", "/", nil)
	rec := httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)

	assert.Equal(t, http.StatusOK, rec.Code)
	assert.Equal(t, "NEW-PRE", rec.Body.String())
	assert.Equal(t, "NEW-PRE-FIN", OneOffPreFinTestObj)
}
Пример #11
0
//---
func TestEarlyResponseMiddleware(t *testing.T) {
	defer func() { Mux = &PatternServeMux{} }()
	defer func() { middlewares = []MiddlewareInterface{} }()
	Pattern("/", &MiddleHandler{})
	Middleware(&Finishware{})

	r, _ := http.NewRequest("GET", "/", nil)
	rec := httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusOK, rec.Code)
	assert.Equal(t, "Ending request early.", rec.Body.String())
}
Пример #12
0
func TestBasicMiddleware(t *testing.T) {
	defer func() { Mux = &PatternServeMux{} }()
	defer func() { middlewares = []MiddlewareInterface{} }()
	Pattern("/", &MiddleHandler{})
	Middleware(&Firstware{})

	r, _ := http.NewRequest("GET", "/", nil)
	rec := httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusOK, rec.Code)
	assert.Equal(t, "bar", rec.Body.String())
	assert.Equal(t, "superman", rec.Header().Get("X-post"))
}
Пример #13
0
func TestRequestIsSecure(t *testing.T) {
	in, _ := http.NewRequest("GET", "http://example.com", nil)
	req := NewHttpRequest(in, make(url.Values))
	assert.Equal(t, false, req.IsSecure())

	in, _ = http.NewRequest("GET", "https://secure-example.com", nil)
	req = NewHttpRequest(in, make(url.Values))
	assert.Equal(t, true, req.IsSecure())

	in, _ = http.NewRequest("GET", "example.com", nil)
	in.Header.Add("X-Forwarded-Proto", "httPS")
	req = NewHttpRequest(in, make(url.Values))
	assert.Equal(t, true, req.IsSecure())
}
Пример #14
0
func TestSettingsSetDebug(t *testing.T) {
	assert.Equal(t, false, Debug)

	dictObj := NewDictObj()

	dictObj.Set("debug", true)
	assert.Equal(t, true, Debug)

	dictObj.Set("debugggger", false)
	assert.Equal(t, true, Debug)

	dictObj.Set("debug", false)
	assert.Equal(t, false, Debug)
}
Пример #15
0
func TestDictObjJsonData(t *testing.T) {
	tmp := make(map[string]interface{})

	err := json.Unmarshal([]byte(`{"intOne":1,"intTwo":23456789}`), &tmp)
	if err != nil {
		t.Errorf("Json unmarshal error: %q", err)
	}

	dictObj := NewDictObj()

	dictObj.Set("a", tmp["intOne"])
	dictObj.Set("b", tmp["intTwo"])

	assert.Equal(t, 1, dictObj.Int("a"))
	assert.Equal(t, 23456789, dictObj.Int("b"))
}
Пример #16
0
func TestFinishMiddleware(t *testing.T) {
	defer func() { Mux = &PatternServeMux{} }()
	defer func() { middlewares = []MiddlewareInterface{} }()
	Pattern("/", &MiddleHandler{})

	// Finish should not allow Firstware to be called.
	Middleware(&Finishware{})
	Middleware(&Firstware{})

	r, _ := http.NewRequest("GET", "/", nil)
	rec := httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusOK, rec.Code)
	assert.Equal(t, "bar", rec.Body.String())
	assert.Equal(t, "FIRST", rec.Header().Get("X-post"))

}
Пример #17
0
func TestDictObjArray(t *testing.T) {
	tmp := make(map[string]interface{})

	err := json.Unmarshal([]byte(`{"first":["one", "two", "three"], "second":["1", "2"]}`), &tmp)
	if err != nil {
		t.Errorf("Json unmarshal error: %q", err)
	}

	dictObj := NewDictObj()
	dictObj.Set("a", tmp["first"])

	assert.Equal(t, tmp["first"].([]interface{}), dictObj.Array("a"))
	assert.Equal(t, tmp["second"].([]interface{}), dictObj.Array("b", tmp["second"].([]interface{})))

	assert.Panic(t, "Array received too many args: [2]", func() {
		dictObj.Array("z", nil, nil)
	})
}
Пример #18
0
func TestDictObjString(t *testing.T) {
	dictObj := NewDictObj()

	dictObj.Set("a", "AAA")
	dictObj.Set("b", "BBB")

	assert.Equal(t, "AAA", dictObj.String("a"))
	assert.Equal(t, "BBB", dictObj.String("b"))

	dictObj.Set("a", "not AAA")

	assert.Equal(t, "not AAA", dictObj.String("a"))
	assert.Equal(t, "CCC", dictObj.String("c", "CCC"))

	assert.Panic(t, "String received too many args: [2]", func() {
		dictObj.String("z", "default", "bad")
	})
}
Пример #19
0
func TestDictObjBool(t *testing.T) {
	dictObj := NewDictObj()

	dictObj.Set("a", true)
	dictObj.Set("b", false)

	assert.Equal(t, true, dictObj.Bool("a"))
	assert.Equal(t, false, dictObj.Bool("b"))
	assert.Equal(t, true, dictObj.Bool("c", true))
	assert.Equal(t, false, dictObj.Bool("d", false))

	dictObj.Set("a", false)
	assert.Equal(t, false, dictObj.Bool("a"))

	assert.Panic(t, "Bool received too many args: [3]", func() {
		dictObj.Bool("z", true, false, true)
	})
}
Пример #20
0
func TestDictObjInt(t *testing.T) {
	dictObj := NewDictObj()

	dictObj.Set("a", 1)
	dictObj.Set("b", 12345678901234)

	assert.Equal(t, 1, dictObj.Int("a"))
	assert.Equal(t, 12345678901234, dictObj.Int("b"))

	dictObj.Set("a", 2)

	assert.Equal(t, 2, dictObj.Int("a"))
	assert.Equal(t, 55, dictObj.Int("c", 55))

	assert.Panic(t, "Int received too many args: [2]", func() {
		dictObj.Int("z", 12, 34)
	})
}
Пример #21
0
func TestDictObjFloat(t *testing.T) {
	dictObj := NewDictObj()

	dictObj.Set("a", 1.0)
	dictObj.Set("b", 12345678901234.56789)

	assert.Equal(t, 1.0, dictObj.Float("a"))
	assert.Equal(t, 12345678901234.56789, dictObj.Float("b"))

	dictObj.Set("a", 222.333)

	assert.Equal(t, 222.333, dictObj.Float("a"))
	assert.Equal(t, 55.667788, dictObj.Float("c", 55.667788))

	assert.Panic(t, "Float received too many args: [2]", func() {
		dictObj.Float("z", 12.23, 33.22)
	})
}
Пример #22
0
func TestHandlerErrors(t *testing.T) {
	defer func() { Mux = &PatternServeMux{} }()
	Pattern("/err/", &ErrHandler{})
	Pattern("/custom/", &CustomErrHandler{})

	r, _ := http.NewRequest("GET", "/err/", nil)
	rec := httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusInternalServerError, rec.Code)
	assert.Equal(t, http.StatusText(500), rec.Body.String())

	r, _ = http.NewRequest("GET", "/custom/", nil)
	rec = httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, 400, rec.Code)
	assert.Equal(t, `{"hello": "world"}`, rec.Body.String())
	assert.Equal(t, "application/json", rec.Header().Get("Content-Type"))
}
Пример #23
0
func TestRequestGetValueTypes(t *testing.T) {
	in, _ := http.NewRequest("GET", "example.com?one=foo&two=222&three=11&four=2.0", nil)
	req := NewHttpRequest(in, make(url.Values))

	vs := req.GetString("one")
	assert.Equal(t, "foo", vs)

	vs = req.GetString("two")
	assert.Equal(t, "222", vs)

	vi := req.GetInt("three")
	assert.Equal(t, int64(11), vi)

	vf := req.GetFloat("four")
	assert.Equal(t, 2.0, vf)

	vs = req.GetString("five", "hello")
	assert.Equal(t, "hello", vs)
}
Пример #24
0
func TestHandlerMethods(t *testing.T) {
	defer func() { Mux = &PatternServeMux{} }()
	Pattern("/", &AlmostBaseHandler{})

	methods := []string{"HEAD", "GET", "POST", "PUT", "PATCH", "DELETE", "OPTIONS"}
	for _, meth := range methods {
		r, _ := http.NewRequest(meth, "/", nil)
		rec := httptest.NewRecorder()
		Mux.ServeHTTP(rec, r)
		assert.Equal(t, http.StatusMethodNotAllowed, rec.Code)
	}
}
Пример #25
0
func TestHandlerHead(t *testing.T) {
	defer func() { Mux = &PatternServeMux{} }()
	Pattern("/", &GetHandler{})

	r, _ := http.NewRequest("GET", "/", nil)
	rec := httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)

	assert.Equal(t, http.StatusOK, rec.Code)
	assert.Equal(t, "hello", rec.Body.String())

	getResult := rec.Body

	r, _ = http.NewRequest("HEAD", "/", nil)
	rec = httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusOK, rec.Code)

	assert.NotEqual(t, getResult, rec.Body)
	assert.Equal(t, 0, len(rec.Body.String()))
}
Пример #26
0
func TestHandlerGenericResponses(t *testing.T) {
	defer func() { Mux = &PatternServeMux{} }()
	Pattern("/", &GenericHandler{})

	r, _ := http.NewRequest("GET", "/", nil)
	rec := httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusGone, rec.Code)

	r, _ = http.NewRequest("POST", "/", nil)
	rec = httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusNotFound, rec.Code)

	r, _ = http.NewRequest("PUT", "/", nil)
	rec = httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusForbidden, rec.Code)

	r, _ = http.NewRequest("DELETE", "/", nil)
	rec = httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusBadRequest, rec.Code)

	r, _ = http.NewRequest("PATCH", "/", nil)
	rec = httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusNotModified, rec.Code)

	r, _ = http.NewRequest("OPTION", "/", nil)
	rec = httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusInternalServerError, rec.Code)
}
Пример #27
0
func TestRequestFormValueTypes(t *testing.T) {
	in, _ := http.NewRequest("GET", "example.com", nil)
	in.PostForm = make(url.Values)
	in.PostForm.Add("one", "foo")
	in.PostForm.Add("two", "222")
	in.PostForm.Add("three", "11")
	in.PostForm.Add("four", "2.0")
	req := NewHttpRequest(in, make(url.Values))

	vs := req.FormString("one")
	assert.Equal(t, "foo", vs)

	vs = req.FormString("two")
	assert.Equal(t, "222", vs)

	vi := req.FormInt("three")
	assert.Equal(t, int64(11), vi)

	vf := req.FormFloat("four")
	assert.Equal(t, 2.0, vf)

	vs = req.FormString("five", "hello")
	assert.Equal(t, "hello", vs)

	vs = req.FormString("six")
	assert.Equal(t, "", vs)

	vi = req.FormInt("seven")
	assert.Equal(t, int64(0), vi)

	vf = req.FormFloat("eight")
	assert.Equal(t, 0.0, vf)

	vi = req.FormInt("nine", 22)
	assert.Equal(t, int64(22), vi)

	vf = req.FormFloat("ten", 10.10)
	assert.Equal(t, 10.10, vf)
}
Пример #28
0
func TestHandlerAllowedMethods(t *testing.T) {
	defer func() { Mux = &PatternServeMux{} }()
	Pattern("/allowed/", &AllowedHandler{})

	r, _ := http.NewRequest("GET", "/allowed/", nil)
	rec := httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusOK, rec.Code)

	r, _ = http.NewRequest("POST", "/allowed/", nil)
	rec = httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusOK, rec.Code)

	r, _ = http.NewRequest("PUT", "/allowed/", nil)
	rec = httptest.NewRecorder()
	Mux.ServeHTTP(rec, r)
	assert.Equal(t, http.StatusMethodNotAllowed, rec.Code)

	// TODO: Still need to fix this sometime down the road.
	//assert.Equal(t, "GET,POST", rec.Header().Get("Allow"))
}
Пример #29
0
func TestSettingsSetFromEnv(t *testing.T) {
	dictObj := NewDictObj()

	dictObj.SetFromEnv("a", "bad_key_here", true)
	assert.Equal(t, true, dictObj.Bool("a"))

	dictObj.SetFromEnv("a", "bad_key_here", false)
	assert.Equal(t, false, dictObj.Bool("a"))

	dictObj.SetFromEnv("b", "no_args")
	assert.Equal(t, false, dictObj.Bool("b"))

	dictObj.SetFromEnv("debug", "bad_key_here222", true)
	assert.Equal(t, true, Debug)

	if tempPath, ok := syscall.Getenv("PATH"); ok {
		dictObj.SetFromEnv("mypath", "PATH", "NA")
		assert.Equal(t, tempPath, dictObj.String("mypath", "NOT NA!!"))
	}

	assert.Panic(t, "SetFromEnv received too many args: [2]", func() {
		dictObj.SetFromEnv("z", "bbaadddddd", false, true)
	})
}
Пример #30
0
func TestRequestPathValue(t *testing.T) {
	in, _ := http.NewRequest("GET", "example.com", nil)
	path := make(url.Values)
	path.Add(":one", "foo")
	path.Add(":two", "bar")
	req := NewHttpRequest(in, path)

	v, ok := req.PathValue(":one")
	assert.Equal(t, true, ok)
	assert.Equal(t, "foo", v)

	v, ok = req.PathValue(":two")
	assert.Equal(t, true, ok)
	assert.Equal(t, "bar", v)

	v, ok = req.PathValue(":foobar")
	assert.Equal(t, false, ok)
	assert.Equal(t, "", v)
}