kataras / iris

The fastest HTTP/2 Go Web Framework. New, modern and easy to learn. Fast development with Code you control. Unbeatable cost-performance ratio :rocket:
https://www.iris-go.com
BSD 3-Clause "New" or "Revised" License
25.23k stars 2.47k forks source link

test fail with `net/http/httptest` #1518

Closed axetroy closed 4 years ago

axetroy commented 4 years ago

Hi.

I used to write a project using gin, and now I am going to use iris

After my rewriting is completed, everything is fine, and it can be compiled and run normally

Only test cases fail

Get 1 &{200 map[]  false <nil> map[] false}
Get 2 &{POST /v1/news HTTP/1.1 1 1 map[Authorization:[Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOiJNVGc0TWprNE5UTTBOVGs1TURBd01EWTAiLCJhdWQiOiIxODgyOTg1MzQ1OTkwMDAwNjQiLCJleHAiOjE1ODk3NTc2NTMsImp0aSI6IjE4ODI5ODUzNDU5OTAwMDA2NCIsImlhdCI6MTU4OTczNjA1MywiaXNzIjoiYWRtaW4iLCJuYmYiOjE1ODk3MzYwNTN9.FiC0PN693YsVS0_7dMkuqYqXFBCieQs2avA6wxfehvo]] {0xc000678f60} 0x4441d00 59 [] false  map[] map[] <nil> map[]   <nil> <nil> <nil> 0xc00003c118}
Get 3 &{{0 0} <nil> <nil> <nil> <nil> <nil> map[]}
--- FAIL: TestCreateRouter (0.06s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x461a046]

goroutine 66 [running]:
testing.tRunner.func1.1(0x4ad0f40, 0x55f0610)
    /usr/local/go/src/testing/testing.go:940 +0x2f5
testing.tRunner.func1(0xc0004b2c60)
    /usr/local/go/src/testing/testing.go:943 +0x3f9
panic(0x4ad0f40, 0x55f0610)
    /usr/local/go/src/runtime/panic.go:969 +0x166
github.com/kataras/iris/v12/core/router.(*Router).ServeHTTP(0xc00019a280, 0x4e01140, 0xc000200480, 0xc000532b00)
    /Users/axetroy/go/src/github.com/axetroy/go-server/vendor/github.com/kataras/iris/v12/core/router/router.go:231 +0x1e6
github.com/axetroy/mocker.(*Mocker).Request(0xc0001c3fc0, 0x4c07a5d, 0x4, 0x4c1290e, 0x8, 0xc0000dd580, 0x3b, 0x40, 0xc000149d40, 0xc000234000)
    /Users/axetroy/go/src/github.com/axetroy/go-server/vendor/github.com/axetroy/mocker/mocker.go:36 +0x322
github.com/axetroy/mocker.(*Mocker).Post(0xc0001c3fc0, 0x4c1290e, 0x8, 0xc0000dd580, 0x3b, 0x40, 0xc000149d40, 0x1)
    /Users/axetroy/go/src/github.com/axetroy/go-server/vendor/github.com/axetroy/mocker/mocker.go:58 +0xe7
github.com/axetroy/go-server/internal/app/admin_server/controller/news_test.TestCreateRouter(0xc0004b2c60)
    /Users/axetroy/go/src/github.com/axetroy/go-server/internal/app/admin_server/controller/news/create_test.go:117 +0x2af
testing.tRunner(0xc0004b2c60, 0x4c9a5a8)
    /usr/local/go/src/testing/testing.go:991 +0xdc
created by testing.(*T).Run
    /usr/local/go/src/testing/testing.go:1042 +0x357

According to the stack information, I found here

截屏2020-05-1801 25 08

It looks like everything is ok and there are no null pointers

The same test case is possible under the gin framework

I know that iris has a testing framework, but due to the high cost of migration, it is unlikely to change

axetroy commented 4 years ago

edit:

it seems mainHandler property of router is nil

I create a router via app := iris.New()

It should be compatible with http.Handler

It should be used for net/http/httptest directly, but for now, it is not

kataras commented 4 years ago

@axetroy you use use the app.Build() first, that builds the main handler:

app := iris.New()
// your routes here
app.Build()

app.ServeHTTP(w,r) is available now.

However I highly recommend using the github.com/kataras/iris/v12/httptest instead. Gin does not have its own testing library, Iris is using the great httpexpect library to provide its own helpers to make your life easier, example with basic authentication like yours: https://github.com/kataras/iris/blob/c5b6ff1dd14165f3775c2f1c6558bdac79ba8a76/_examples/testing/httptest/main_test.go#L20

axetroy commented 4 years ago

Thx. it works for me

kataras commented 4 years ago

You're welcome @axetroy!