公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

在使用Go语言编写Web应用程序时,我们经常会用到gin框架来处理HTTP请求和响应。在进行单元测试时,我们需要对代码进行覆盖测试,以保证代码的质量和稳定性。然而,针对gin的Context.Redirect方法的单元测试在处理GET请求时非常适用,但在处理POST请求时却不太适用。在本文中,php小编苹果将会详细解释为什么这个问题会出现,并提供一些解决方案来进行POST请求的单元测试。

问题内容

我希望我的服务器将特定端点重定向到另一台服务器。该端点可以是 getted 或 posted。在这两种情况下,http 响应代码都应为 302。如果我在此代码上使用 curl ,它确实在两种情况下都显示响应代码 302,并且 curl -l 正确遵循重定向。哇哦。

但是

我的单元测试使用httptest.newrecorder()来捕获信息,但它仅适用于get,不适用于post。因此,当我知道实际的重定向正在工作时,我需要弄清楚如何让单元测试工作。失败测试显示http响应代码是200而不是302(http.statusfound)。

$ go run foo.go
post code 200
get code 302

登录后复制

这是独立测试。

package main

import (
    "net/http"
    "net/http/httptest"
    "github.com/gin-gonic/gin"
)

func main() {
    gin.setmode(gin.releasemode)
    {
        w := httptest.newrecorder()
        context, _ := gin.createtestcontext(w)
        context.request = httptest.newrequest("post", "http://localhost:23632/foobar", nil)
        context.redirect(http.statusfound, "http://foobar.com")

        print("post code ",w.code,"\n")
    }

    {
        w := httptest.newrecorder()
        context, _ := gin.createtestcontext(w)
        context.request = httptest.newrequest("get", "http://localhost:23632/foobar", nil)
        context.redirect(http.statusfound, "http://foobar.com")

        print("get code ",w.code,"\n")
    }
}

登录后复制

当我在实际应用程序(未显示)上执行 curl post 时,我发现它正在工作:

curl -v -XPOST localhost:23632/foobar
* About to connect() to localhost port 23632 (#0)
*   Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 23632 (#0)
> POST /foobar HTTP/1.1
> User-Agent: curl/7.29.0
> Host: localhost:23632
> Accept: */*
>
< HTTP/1.1 302 Found
< Location: http://foobar.com
< Vary: Origin
< Date: Tue, 23 May 2023 22:38:42 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact

登录后复制

解决方法

tl;dr

解决方法是在 context.redirect 之后显式调用 context.writer.writeheadernow。

说明

这是使用从 gin.createtestcontext 返回的 gin 上下文的一个极端情况。

对于 get 请求,gin 最终会调用 http.redirect ,它将向响应写入一个简短的 html 正文(类似于 found ),从而导致要写入响应的状态代码。

对于 post 请求,http.redirect 不会写入短 html 正文,并且状态代码没有机会写入响应。

参见http 的实现.重定向。根据源码,如果之前设置了content-type header,那么get请求也会出现同样的问题:

{
    w := httptest.newrecorder()
    context, _ := gin.createtestcontext(w)
    context.request = httptest.newrequest("get", "http://localhost:23632/foobar", nil)
+   context.header("content-type", "text/html")
    context.redirect(http.statusfound, "http://foobar.com")
    print("get code ", w.code, "\n")
  }

登录后复制

解决方法是显式调用 context.writer.writeheadernow:

{
    w := httptest.NewRecorder()
    context, _ := gin.CreateTestContext(w)
    context.Request = httptest.NewRequest("POST", "http://localhost:23632/foobar", nil)
    context.Redirect(http.StatusFound, "http://foobar.com")
+   context.Writer.WriteHeaderNow()

    print("POST code ", w.Code, "\n")
  }

登录后复制

gin 本身使用相同的解决方法。请参阅 testcontextrenderredirectwithrelativepath。

真正的服务器应用程序不会遇到同样的问题,因为 (*engine).handlehttprequest 将为我们调用 writeheadernow (请参阅 源代码)。这就是为什么我称其为“极端情况”而不是“错误”。

分享到:
标签:Go语言
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定