I have some tests that inject headers into Echo like this:
func test() {
request := httptest.NewRequest(http.MethodGet, "/", http.NoBody)
recorder := httptest.NewRecorder()
recorder.HeaderMap.Add("If-None-Match", "\"d41d8cd98f00b204e9800998ecf8427e\"")
context := inst.NewContext(request, recorder)
testFunc(context)
fmt.Printf("Status: %d", context.Response().Status)
}
func testFunc(ctx echo.Context) {
ifNoneMatch := ctx.Response().Header().Get(headers.IfNoneMatch)
if !util.IsEmpty(ifNoneMatch) && etag == ifNoneMatch {
ctx.Response().WriteHeader(304)
}
}
My current solution works but, as HeaderMap
is deprecated, I'm trying to find a better way to do this. I've tried injecting the header into Result
by doing Result().Header.Add("If-None-Match", "\"d41d8cd98f00b204e9800998ecf8427e\"")
but it doesn't seem to show up when calling context.Response().Header()
. Is there any way to do this?
Instead of using HeaderMap() which is deprecated, you can use this:
request.Header().Set("Header-name", "Any header")