mirror of
https://github.com/kataras/iris.git
synced 2025-12-17 18:07:01 +00:00
fix #2158 and more
This commit is contained in:
@@ -146,65 +146,65 @@ func TestFuncResult(t *testing.T) {
|
||||
e := httptest.New(t, app)
|
||||
|
||||
e.GET("/text").Expect().Status(iris.StatusOK).
|
||||
Body().Equal("text")
|
||||
Body().IsEqual("text")
|
||||
|
||||
e.GET("/status").Expect().Status(iris.StatusBadGateway)
|
||||
|
||||
e.GET("/text/with/status/ok").Expect().Status(iris.StatusOK).
|
||||
Body().Equal("OK")
|
||||
Body().IsEqual("OK")
|
||||
|
||||
e.GET("/status/with/text/not/ok/first/second").Expect().Status(iris.StatusForbidden).
|
||||
Body().Equal("NOT_OK_firstsecond")
|
||||
Body().IsEqual("NOT_OK_firstsecond")
|
||||
// Author's note: <-- if that fails means that the last binder called for both input args,
|
||||
// see path_param_binder.go
|
||||
|
||||
e.GET("/text/and/content/type").Expect().Status(iris.StatusOK).
|
||||
ContentType("text/html", "utf-8").
|
||||
Body().Equal("<b>text</b>")
|
||||
Body().IsEqual("<b>text</b>")
|
||||
|
||||
e.GET("/custom/response").Expect().Status(iris.StatusOK).
|
||||
ContentType("text/html", "utf-8").
|
||||
Body().Equal("<b>text</b>")
|
||||
Body().IsEqual("<b>text</b>")
|
||||
e.GET("/custom/response/with/status/ok").Expect().Status(iris.StatusOK).
|
||||
ContentType("text/html", "utf-8").
|
||||
Body().Equal("<b>OK</b>")
|
||||
Body().IsEqual("<b>OK</b>")
|
||||
e.GET("/custom/response/with/status/not/ok").Expect().Status(iris.StatusInternalServerError).
|
||||
ContentType("text/html", "utf-8").
|
||||
Body().Equal("<b>internal server error</b>")
|
||||
Body().IsEqual("<b>internal server error</b>")
|
||||
|
||||
expectedResultFromCustomStruct := map[string]interface{}{
|
||||
"name": "Iris",
|
||||
"age": 2,
|
||||
}
|
||||
e.GET("/custom/struct").Expect().Status(iris.StatusOK).
|
||||
JSON().Equal(expectedResultFromCustomStruct)
|
||||
JSON().IsEqual(expectedResultFromCustomStruct)
|
||||
e.GET("/custom/struct/with/status/not/ok").Expect().Status(iris.StatusInternalServerError).
|
||||
JSON().Equal(expectedResultFromCustomStruct)
|
||||
JSON().IsEqual(expectedResultFromCustomStruct)
|
||||
e.GET("/custom/struct/with/content/type").Expect().Status(iris.StatusOK).
|
||||
ContentType("text/xml", "utf-8")
|
||||
e.GET("/custom/struct/with/error").Expect().Status(iris.StatusOK).
|
||||
JSON().Equal(expectedResultFromCustomStruct)
|
||||
JSON().IsEqual(expectedResultFromCustomStruct)
|
||||
e.GET("/custom/struct/with/error").WithQuery("err", true).Expect().
|
||||
Status(iris.StatusBadRequest). // the default status code if error is not nil
|
||||
// the content should be not JSON it should be the status code's text
|
||||
// it will fire the error's text
|
||||
Body().Equal("omit return of testCustomStruct and fire error")
|
||||
Body().IsEqual("omit return of testCustomStruct and fire error")
|
||||
|
||||
e.GET("/custom/error/as/dispatcher").Expect().
|
||||
Status(iris.StatusBadRequest). // the default status code if error is not nil
|
||||
// the content should be not JSON it should be the status code's text
|
||||
// it will fire the error's text
|
||||
JSON().Equal(err{iris.StatusBadRequest, "this is my error as json"})
|
||||
JSON().IsEqual(err{iris.StatusBadRequest, "this is my error as json"})
|
||||
|
||||
// its result is nil should give an empty response but content-type is set correctly.
|
||||
e.GET("/custom/nil/typed").Expect().
|
||||
Status(iris.StatusOK).ContentType(context.ContentJSONHeaderValue).Body().Empty()
|
||||
Status(iris.StatusOK).ContentType(context.ContentJSONHeaderValue).Body().IsEmpty()
|
||||
e.GET("/custom/nil/typed/ptr").Expect().
|
||||
Status(iris.StatusOK).ContentType(context.ContentJSONHeaderValue).Body().Empty()
|
||||
Status(iris.StatusOK).ContentType(context.ContentJSONHeaderValue).Body().IsEmpty()
|
||||
e.GET("/custom/nil/map").Expect().
|
||||
Status(iris.StatusOK).ContentType(context.ContentJSONHeaderValue).Body().Empty()
|
||||
Status(iris.StatusOK).ContentType(context.ContentJSONHeaderValue).Body().IsEmpty()
|
||||
e.GET("/custom/nil/struct").Expect().
|
||||
Status(iris.StatusOK).ContentType(context.ContentJSONHeaderValue).Body().Empty()
|
||||
Status(iris.StatusOK).ContentType(context.ContentJSONHeaderValue).Body().IsEmpty()
|
||||
}
|
||||
|
||||
type (
|
||||
@@ -246,20 +246,20 @@ func TestPreflightResult(t *testing.T) {
|
||||
|
||||
expected1 := testPreflightResponse{Code: httptest.StatusOK, Message: "OK"}
|
||||
e.POST("/").WithJSON(testPreflightRequest{FailCode: expected1.Code}).
|
||||
Expect().Status(httptest.StatusOK).JSON().Equal(expected1)
|
||||
Expect().Status(httptest.StatusOK).JSON().IsEqual(expected1)
|
||||
|
||||
expected2 := testPreflightResponse{Code: httptest.StatusBadRequest, Message: "Bad Request"}
|
||||
e.POST("/").WithJSON(testPreflightRequest{FailCode: expected2.Code}).
|
||||
Expect().Status(httptest.StatusBadRequest).JSON().Equal(expected2)
|
||||
Expect().Status(httptest.StatusBadRequest).JSON().IsEqual(expected2)
|
||||
|
||||
// Test error returned from Preflight.
|
||||
e.POST("/").WithJSON(testPreflightRequest{FailCode: httptest.StatusInternalServerError}).
|
||||
Expect().Status(httptest.StatusBadRequest).Body().Equal("custom error")
|
||||
Expect().Status(httptest.StatusBadRequest).Body().IsEqual("custom error")
|
||||
|
||||
// Can be done without Preflight as the second output argument can be a status code.
|
||||
expected4 := testOutput{Name: "my_name"}
|
||||
e.POST("/alternative").WithJSON(testInput{expected4.Name}).
|
||||
Expect().Status(httptest.StatusAccepted).JSON().Equal(expected4)
|
||||
Expect().Status(httptest.StatusAccepted).JSON().IsEqual(expected4)
|
||||
}
|
||||
|
||||
func TestResponseErr(t *testing.T) {
|
||||
@@ -280,5 +280,5 @@ func TestResponseErr(t *testing.T) {
|
||||
})
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/").Expect().Status(iris.StatusBadGateway).Body().Equal("response error")
|
||||
e.GET("/").Expect().Status(iris.StatusBadGateway).Body().IsEqual("response error")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user