-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why GetBy can't be accessed, but PostBy can #1487
Comments
Hello @weigun, it seems that the route is registered correctly, there is nothing wrong here, are you sure it finds POST instead of GET? Because I can't re-produce your issue, take look at the example and test below: // file: main.go
package main
import (
"github.com/kataras/iris/v12"
"github.com/kataras/iris/v12/mvc"
)
func main() {
app := newApp()
app.Run(iris.Addr(":8080"), iris.WithoutServerError(iris.ErrServerClosed))
}
func newApp() *iris.Application {
app := iris.New().Configure(iris.WithFireMethodNotAllowed)
mvc.New(app.Party("/project")).Handle(new(testControllerGetBy))
return app
}
type testCustomStruct struct {
Name string `json:"name" xml:"name"`
Age int `json:"age" xml:"age"`
}
type testControllerGetBy struct{}
func (c *testControllerGetBy) GetBy(age int64) *testCustomStruct {
return &testCustomStruct{
Age: int(age),
Name: "name",
}
} And its main_test.go: package main
import (
"testing"
"github.com/kataras/iris/v12/httptest"
)
func TestControllerGetBy(t *testing.T) {
// Tests only GetBy.
app := newApp()
e := httptest.New(t, app)
e.GET("/project/42").Expect().Status(httptest.StatusOK).
JSON().Equal(&testCustomStruct{Age: 42, Name: "name"})
e.POST("/project/42").Expect().Status(httptest.StatusMethodNotAllowed)
} Output: PS C:\mygopath\src\github.com\kataras\iris\_examples\1487> go test -v
=== RUN TestControllerGetBy
--- PASS: TestControllerGetBy (0.00s)
PASS
ok github.com/kataras/iris/v12/_examples/1487 0.141s Tested on v12 (and master) branch |
I'm very sorry for the late reply. I'll test it again today. |
I found that the problem stems from the ''AllowMethods'' method.
And its main_test.go:
Output:
|
@weigun that's the point of So, the correct test is that: func TestControllerGetByWithAllowMethods(t *testing.T) {
app := iris.New()
app.Configure(iris.WithFireMethodNotAllowed)
// ^ this 405 status will not be fired on POST: project/... because of
// .AllowMethods, but it will on PUT.
New(app.Party("/project").AllowMethods(iris.MethodGet, iris.MethodPost)).
Handle(new(testControllerGetBy))
e := httptest.New(t, app)
e.GET("/project/42").Expect().Status(httptest.StatusOK).
JSON().Equal(&testCustomStruct{Age: 42, Name: "name"})
e.POST("/project/42").Expect().Status(httptest.StatusOK)
e.PUT("/project/42").Expect().Status(httptest.StatusMethodNotAllowed)
}
=== RUN TestControllerGetByWithAllowMethods
--- PASS: TestControllerGetByWithAllowMethods (0.00s)
PASS
ok github.com/kataras/iris/v12/mvc 0.151s At the current state of the expected status equal to:
"200 OK"
but got:
"405 Method Not Allowed" This is fixed on master branch, future v12.2.0 release. So you did well opening this issue, as MVC had conflicts with the |
Just as I was about to reply to you that the test was still failing, I found that you had updated the comments. It's great that the problem is fixed. |
…1487 Former-commit-id: 5f4abd4a8c6528d666d91315fa515a4a607326fc
Hello everyone, we are currently experiencing a strange problem and would like to ask for your help, as follows.
GET, visit http://127.0.0.1:8080/project/1, return 404
POST, visit http://127.0.0.1:8080/project/1, return 200
Only GetBy(id int64) is defined in the controller, not PostBy.
The code is as follows.
iris version:V12
controller:
route defined:
debug log here:
The text was updated successfully, but these errors were encountered: