gin
gin copied to clipboard
fix missing route params for CreateTestContext (#2778)
This PR trying to fix the issue we met, which exact same with https://github.com/gin-gonic/gin/issues/2778
If you guys have better idea to fix this, just let me know.
Codecov Report
Merging #2803 (621fc9d) into master (b57163a) will increase coverage by
0.00%. The diff coverage is100.00%.
@@ Coverage Diff @@
## master #2803 +/- ##
=======================================
Coverage 98.37% 98.38%
=======================================
Files 43 43
Lines 3148 3153 +5
=======================================
+ Hits 3097 3102 +5
Misses 38 38
Partials 13 13
| Flag | Coverage Δ | |
|---|---|---|
| go-1.15 | 98.38% <100.00%> (+<0.01%) |
:arrow_up: |
| go-1.16 | 98.35% <100.00%> (+<0.01%) |
:arrow_up: |
| go-1.17 | 98.28% <100.00%> (+<0.01%) |
:arrow_up: |
| go-1.18 | 98.28% <100.00%> (+<0.01%) |
:arrow_up: |
| macos-latest | 98.38% <100.00%> (+<0.01%) |
:arrow_up: |
| nomsgpack | 98.35% <100.00%> (+<0.01%) |
:arrow_up: |
| ubuntu-latest | 98.38% <100.00%> (+<0.01%) |
:arrow_up: |
Flags with carried forward coverage won't be shown. Click here to find out more.
| Impacted Files | Coverage Δ | |
|---|---|---|
| gin.go | 99.18% <100.00%> (ø) |
|
| test_helpers.go | 100.00% <100.00%> (ø) |
Continue to review full report at Codecov.
Legend - Click here to learn more
Δ = absolute <relative> (impact),ø = not affected,? = missing dataPowered by Codecov. Last update b57163a...621fc9d. Read the comment docs.
Had the same problem when trying to follow the examples.
The fix you outline would work but I think at that point especially with the need to create a separate engine to get the valid maxParam value one could just use a different approach using ServeHTTP and forget CreateTestContext.
Tests could be written as follows based on your example:
w := httptest.NewRecorder() engine := New()
// At this point add the route(s) to test engine.GET("/:action/:name", func(ctx *Context) { ctx.String(http.StatusOK, "%s %s", ctx.Param("action"), ctx.Param("name")) }) r = http.NewRequest(http.MethodGet, "/hello/gin", nil)
engine.ServeHTTP(w,r)
assert.Equal(t, http.StatusOK, w.Code) assert.Equal(t, "hello gin", w.Body.String())
Admittedly this does not use the test context at all but I wonder if using CreateTestContext is even a good idea one could say that ServeHTTP is a better approach. Like the context method this does not start the engine.
@RoCry Please fix the conflicts.
Is there any plan to release this soon?
Or is there any workaround?
we will release the feature in v1.8.2
https://github.com/gin-gonic/gin/releases/tag/v1.8.2