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:
@@ -73,7 +73,7 @@ func TestBasicAuthUseRouter(t *testing.T) {
|
||||
for username, password := range users {
|
||||
// Test pass authentication and route found.
|
||||
e.GET("/").WithBasicAuth(username, password).Expect().
|
||||
Status(httptest.StatusOK).Body().Equal(fmt.Sprintf("Hello, %s!", username))
|
||||
Status(httptest.StatusOK).Body().IsEqual(fmt.Sprintf("Hello, %s!", username))
|
||||
e.GET("/user_json").WithBasicAuth(username, password).Expect().
|
||||
Status(httptest.StatusOK).JSON().Object().ContainsMap(iris.Map{
|
||||
"username": username,
|
||||
@@ -83,7 +83,7 @@ func TestBasicAuthUseRouter(t *testing.T) {
|
||||
Equal(fmt.Sprintf("%s\n%s\n%s", "Basic Authentication", username, password))
|
||||
|
||||
// Test empty auth.
|
||||
e.GET("/").Expect().Status(httptest.StatusUnauthorized).Body().Equal("Unauthorized")
|
||||
e.GET("/").Expect().Status(httptest.StatusUnauthorized).Body().IsEqual("Unauthorized")
|
||||
// Test invalid auth.
|
||||
e.GET("/").WithBasicAuth(username, "invalid_password").Expect().
|
||||
Status(httptest.StatusForbidden)
|
||||
@@ -93,14 +93,14 @@ func TestBasicAuthUseRouter(t *testing.T) {
|
||||
// Test different method, it should pass the authentication (no stop on 401)
|
||||
// but it doesn't fire the GET route, instead it gives 405.
|
||||
e.POST("/").WithBasicAuth(username, password).Expect().
|
||||
Status(httptest.StatusMethodNotAllowed).Body().Equal("Method Not Allowed")
|
||||
Status(httptest.StatusMethodNotAllowed).Body().IsEqual("Method Not Allowed")
|
||||
|
||||
// Test pass the authentication but route not found.
|
||||
e.GET("/notfound").WithBasicAuth(username, password).Expect().
|
||||
Status(httptest.StatusNotFound).Body().Equal("Not Found")
|
||||
Status(httptest.StatusNotFound).Body().IsEqual("Not Found")
|
||||
|
||||
// Test empty auth.
|
||||
e.GET("/notfound").Expect().Status(httptest.StatusUnauthorized).Body().Equal("Unauthorized")
|
||||
e.GET("/notfound").Expect().Status(httptest.StatusUnauthorized).Body().IsEqual("Unauthorized")
|
||||
// Test invalid auth.
|
||||
e.GET("/notfound").WithBasicAuth(username, "invalid_password").Expect().
|
||||
Status(httptest.StatusForbidden)
|
||||
@@ -114,7 +114,7 @@ func TestBasicAuthUseRouter(t *testing.T) {
|
||||
|
||||
// Test pass and route found.
|
||||
sub.GET("/").WithBasicAuth(username, password).Expect().
|
||||
Status(httptest.StatusOK).Body().Equal(fmt.Sprintf("Static, %s", username))
|
||||
Status(httptest.StatusOK).Body().IsEqual(fmt.Sprintf("Static, %s", username))
|
||||
|
||||
// Test empty auth.
|
||||
sub.GET("/").Expect().Status(httptest.StatusUnauthorized)
|
||||
@@ -126,10 +126,10 @@ func TestBasicAuthUseRouter(t *testing.T) {
|
||||
|
||||
// Test pass the authentication but route not found.
|
||||
sub.GET("/notfound").WithBasicAuth(username, password).Expect().
|
||||
Status(httptest.StatusNotFound).Body().Equal("Not Found")
|
||||
Status(httptest.StatusNotFound).Body().IsEqual("Not Found")
|
||||
|
||||
// Test empty auth.
|
||||
sub.GET("/notfound").Expect().Status(httptest.StatusUnauthorized).Body().Equal("Unauthorized")
|
||||
sub.GET("/notfound").Expect().Status(httptest.StatusUnauthorized).Body().IsEqual("Unauthorized")
|
||||
// Test invalid auth.
|
||||
sub.GET("/notfound").WithBasicAuth(username, "invalid_password").Expect().
|
||||
Status(httptest.StatusForbidden)
|
||||
@@ -142,16 +142,16 @@ func TestBasicAuthUseRouter(t *testing.T) {
|
||||
sub = e.Builder(func(req *httptest.Request) {
|
||||
req.WithURL("http://reset_with_use_router.mydomain.com")
|
||||
})
|
||||
sub.GET("/").Expect().Status(httptest.StatusOK).Body().Equal("with use router\n")
|
||||
sub.POST("/").Expect().Status(httptest.StatusMethodNotAllowed).Body().Equal("Method Not Allowed")
|
||||
sub.GET("/notfound").Expect().Status(httptest.StatusNotFound).Body().Equal("Not Found")
|
||||
sub.GET("/").Expect().Status(httptest.StatusOK).Body().IsEqual("with use router\n")
|
||||
sub.POST("/").Expect().Status(httptest.StatusMethodNotAllowed).Body().IsEqual("Method Not Allowed")
|
||||
sub.GET("/notfound").Expect().Status(httptest.StatusNotFound).Body().IsEqual("Not Found")
|
||||
|
||||
// Test a reset-ed Party (all should pass without auth).
|
||||
sub = e.Builder(func(req *httptest.Request) {
|
||||
req.WithURL("http://reset.mydomain.com")
|
||||
})
|
||||
sub.GET("/").Expect().Status(httptest.StatusOK).Body().Empty()
|
||||
sub.POST("/").Expect().Status(httptest.StatusMethodNotAllowed).Body().Equal("Method Not Allowed")
|
||||
sub.GET("/notfound").Expect().Status(httptest.StatusNotFound).Body().Equal("Not Found")
|
||||
sub.GET("/").Expect().Status(httptest.StatusOK).Body().IsEmpty()
|
||||
sub.POST("/").Expect().Status(httptest.StatusMethodNotAllowed).Body().IsEqual("Method Not Allowed")
|
||||
sub.GET("/notfound").Expect().Status(httptest.StatusNotFound).Body().IsEqual("Not Found")
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,10 +49,10 @@ func TestJWT(t *testing.T) {
|
||||
// Test Header.
|
||||
headerValue := fmt.Sprintf("Bearer %s", token)
|
||||
e.GET("/protected").WithHeader("Authorization", headerValue).Expect().
|
||||
Status(iris.StatusOK).Body().Equal("bar")
|
||||
Status(iris.StatusOK).Body().IsEqual("bar")
|
||||
// Test URL query.
|
||||
e.GET("/protected").WithQuery("token", token).Expect().
|
||||
Status(iris.StatusOK).Body().Equal("bar")
|
||||
Status(iris.StatusOK).Body().IsEqual("bar")
|
||||
|
||||
// Test unauthorized.
|
||||
e.GET("/protected").Expect().Status(iris.StatusUnauthorized)
|
||||
@@ -61,5 +61,5 @@ func TestJWT(t *testing.T) {
|
||||
// Test expired (note checks happen based on second round).
|
||||
time.Sleep(5 * time.Second)
|
||||
e.GET("/protected").WithHeader("Authorization", headerValue).Expect().
|
||||
Status(iris.StatusUnauthorized).Body().Equal("jwt: token expired")
|
||||
Status(iris.StatusUnauthorized).Body().IsEqual("jwt: token expired")
|
||||
}
|
||||
|
||||
@@ -49,28 +49,28 @@ func TestMethodOverrideWrapper(t *testing.T) {
|
||||
|
||||
// Test headers.
|
||||
e.POST("/path").WithHeader("X-HTTP-Method", iris.MethodDelete).Expect().
|
||||
Status(iris.StatusOK).Body().Equal(expectedDelResponse)
|
||||
Status(iris.StatusOK).Body().IsEqual(expectedDelResponse)
|
||||
e.POST("/path").WithHeader("X-HTTP-Method-Override", iris.MethodDelete).Expect().
|
||||
Status(iris.StatusOK).Body().Equal(expectedDelResponse)
|
||||
Status(iris.StatusOK).Body().IsEqual(expectedDelResponse)
|
||||
e.POST("/path").WithHeader("X-Method-Override", iris.MethodDelete).Expect().
|
||||
Status(iris.StatusOK).Body().Equal(expectedDelResponse)
|
||||
Status(iris.StatusOK).Body().IsEqual(expectedDelResponse)
|
||||
|
||||
// Test form field value.
|
||||
e.POST("/path").WithFormField("_method", iris.MethodDelete).Expect().
|
||||
Status(iris.StatusOK).Body().Equal(expectedDelResponse)
|
||||
Status(iris.StatusOK).Body().IsEqual(expectedDelResponse)
|
||||
|
||||
// Test URL Query (although it's the same as form field in this case).
|
||||
e.POST("/path").WithQuery("_method", iris.MethodDelete).Expect().
|
||||
Status(iris.StatusOK).Body().Equal(expectedDelResponse)
|
||||
Status(iris.StatusOK).Body().IsEqual(expectedDelResponse)
|
||||
|
||||
// Test saved original method and
|
||||
// Test without registered "POST" route.
|
||||
e.POST("/path2").WithQuery("_method", iris.MethodDelete).Expect().
|
||||
Status(iris.StatusOK).Body().Equal(expectedDelResponse + iris.MethodPost)
|
||||
Status(iris.StatusOK).Body().IsEqual(expectedDelResponse + iris.MethodPost)
|
||||
|
||||
// Test simple POST request without method override fields.
|
||||
e.POST("/path").Expect().Status(iris.StatusOK).Body().Equal(expectedPostResponse)
|
||||
e.POST("/path").Expect().Status(iris.StatusOK).Body().IsEqual(expectedPostResponse)
|
||||
|
||||
// Test simple DELETE request.
|
||||
e.DELETE("/path").Expect().Status(iris.StatusOK).Body().Equal(expectedDelResponse)
|
||||
e.DELETE("/path").Expect().Status(iris.StatusOK).Body().IsEqual(expectedDelResponse)
|
||||
}
|
||||
|
||||
@@ -57,7 +57,7 @@ func TestRequestID(t *testing.T) {
|
||||
|
||||
e := httptest.New(t, app)
|
||||
e.GET("/default").Expect().Status(httptest.StatusOK).Body().NotEmpty()
|
||||
e.GET("/custom").Expect().Status(httptest.StatusOK).Body().Equal(expectedCustomID)
|
||||
e.GET("/custom_err").Expect().Status(httptest.StatusUnauthorized).Body().Equal(expectedErrMsg)
|
||||
e.GET("/custom_change_id").Expect().Status(httptest.StatusOK).Body().Equal(expectedCustomIDFromOtherMiddleware)
|
||||
e.GET("/custom").Expect().Status(httptest.StatusOK).Body().IsEqual(expectedCustomID)
|
||||
e.GET("/custom_err").Expect().Status(httptest.StatusUnauthorized).Body().IsEqual(expectedErrMsg)
|
||||
e.GET("/custom_change_id").Expect().Status(httptest.StatusOK).Body().IsEqual(expectedCustomIDFromOtherMiddleware)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user