示例#1
0
文件: rest_test.go 项目: xozrc/xo
func TestRestErrorResult(t *testing.T) {
	m := martini.New()
	recorder := httptest.NewRecorder()
	m.Use(rest.RestPostHandler())
	m.Use(func(c martini.Context, res http.ResponseWriter, req *http.Request) {

		err := &rest.RestError{-1000, "12"}
		c.MapTo(err, (*error)(nil))
	})

	m.ServeHTTP(recorder, (*http.Request)(nil))

	if recorder.Code != http.StatusOK {
		t.Error("failed status")
		return
	}

	if recorder.Header().Get("Content-Type") != "application/json; charset=utf-8" {
		t.Error("failed content type")
		return
	}

	var returnObj rest.RestReturnObj

	if err := json.Unmarshal(recorder.Body.Bytes(), &returnObj); err != nil {
		t.Error("json decode failed")
		return
	}

	if returnObj.ErrorCode != -1000 {
		t.Error("error code failed")
		return
	}

}
示例#2
0
文件: rest_test.go 项目: xozrc/xo
func TestNormalHttp(t *testing.T) {
	m := martini.New()
	recorder := httptest.NewRecorder()
	m.Use(rest.RestPostHandler())
	m.ServeHTTP(recorder, (*http.Request)(nil))

	if recorder.Code != http.StatusOK {
		t.Error("failed status")
		return
	}

	if recorder.Body.Len() != 0 {
		t.Error("too much data")
	}
}
示例#3
0
文件: rest_test.go 项目: xozrc/xo
func TestRestResult(t *testing.T) {

	m := martini.New()
	recorder := httptest.NewRecorder()
	m.Use(rest.RestPostHandler())
	m.Use(func(c martini.Context, res http.ResponseWriter, req *http.Request) {

		tempLogin := &RestLoginResult{}
		tempLogin.UserId = 10001
		c.MapTo(tempLogin, (*rest.RestResult)(nil))
	})

	m.ServeHTTP(recorder, (*http.Request)(nil))

	if recorder.Code != http.StatusOK {
		t.Error("failed status")
		return
	}

	var returnObj RestReturnObj

	if err := json.Unmarshal(recorder.Body.Bytes(), &returnObj); err != nil {
		t.Error("json decode failed:" + err.Error())
		return
	}

	if returnObj.ErrorCode != 0 {
		t.Error("error code failed")
		return
	}

	loginResult := returnObj.Result

	if loginResult.UserId != 10001 {
		t.Error("result failed")
		return
	}
}