草庐IT

test_http

全部标签

http - 由对等 GO 重置连接

我正在尝试在GO中执行请求,但我总是收到“连接被对等方重置”错误。以下代码显示了我是如何执行请求的:req,err:=http.NewRequest("GET",url,nil)iferr!=nil{returnnil,err}client:==&http.Client{}resp,err:=client.Do(req)iferr!=nil{returnnil,err}deferresp.Body.Close()fmt.Println(resp.Body)...我收到:Gethttps://example.com:readtcp1.2.3.4:1234->5.6.7.8:5678:re

testing - 我应该使用哪种方法来测试 golang 中的 func main()?

我在main.go中有一个函数main()可以完成这项工作,所有其他函数都在它下面(我没有在这里包括它们)。因此,当我为main中包含的所有funcs编写测试时,我可以测试它们。但是代码覆盖率很低,因为它表明我没有覆盖main函数中的代码。我知道测试库中有一个TestMain函数可以完成这项工作,但我就是不知道如何让它工作,以便测试涵盖funcmain()。下面是我的main()函数,它没有被测试覆盖...funcmain(){c,err:=getConfig()iferr!=nil{log.Fatal(err)}slideshows,err:=getSlideshows(c)ifer

go - http中的Goroutines

Thisquestionalreadyhasananswerhere:Gowebserverrequestsspawnitsowngoroutine?(1个答案)去年关闭。我对http中的goroutines有疑问。在下面的代码中是一个简单的Web服务器。如果有5个人访问服务器,则2个人进入handler1()函数,3个人进入handler2(),golang将创建5个goroutine或我是否需要保留字go?例如gohttp.HandleFunc("/h1",handler1)packagemainimport("fmt""log""net/http")funchandler1(wh

http - 无法在 if..else 条件 : "undefined err go" 中获取变量数据

我正在尝试通过使用if..else条件来使用HTTPGet,但出现错误:undefined:errgo这是我的代码:packagemainimport("fmt""net/http")funcmain(){num:=0ifnum==0{resp,err:=http.Get("https://httpbin.org/get")}else{resp,err:=http.Get("https://google.com")}iferr!=nil{fmt.Println("error")}fmt.Println(resp.StatusCode)}我尝试在调用之前定义变量:varerrerrorv

http - 如何更改 golang 中 http 响应的状态文本,如 200 OK 到 200 {Custom text}

我想在GO中更改给定状态代码的响应文本。怎么做。目前一些流行的状态码的状态文本是这样的:200->确定404->未找到201->已创建我想用我的消息更改文本,例如200->{我的自定义消息} 最佳答案 如果你真的想改变响应,你可以使用golang的net包并实现你自己的类似HTTP的协议(protocol),而不是使用net/http。 关于http-如何更改golang中http响应的状态文本,如200OK到200{Customtext},我们在StackOverflow上找到一个类似

unit-testing - 如何为一小时后函数返回时间编写单元测试

我有以下函数,它在当前时间添加给定的小时数并在中返回新时间funcgetExpiryTime(hourint)*string{constlayout="2006-01-02T15:04:05Z"expiryTime:=time.Now().Local().Add(time.Hour*time.Duration(hour))returnaws.String(expiryTime.Format(layout))}为此功能编写单元测试的最佳方法是什么? 最佳答案 您可以尝试模拟时间提供程序并在您的模拟中设置time.Now()函数以返回您

http - 配置 http :localhost for Golang

这个问题在这里已经有了答案:RungowebapplicationonIIS(2个答案)关闭4年前。我想在Golang中实现一些教程,其中包括net/http库以及其他网络和路由库。此外,我使用本地主机通过IIS测试我的asp.net核心。我已经在我的机器上停止了我的IIS客户端,它仍然将我重定向到IIS主题的本地主机,而不是显示我的golangFprintf消息。我的本地主机上的页面、我当前的iis设置和golang代码可以在下面看到。任何帮助,将不胜感激。http:localhost上的页面我的IIS情况总结(手动停止)packagemainimport"fmt"import"ne

unit-testing - 去单元测试无法加载文件

1、gotestconfig_test.go,总是文件,error:openconf/config.yml:Thesystemcannotfindthepathspecified2、源码ok3、gotestfile如何在不同目录下成功 最佳答案 测试期间您的工作目录不同。您可能需要动态计算配置目录路径的解决方法,例如:http://www.kaihag.com/external-assets-working-directories-and-go/ 关于unit-testing-去单元测试

unit-testing - 错误 : suite. go:61: test paniced: reflect: Call with too few input arguments

我正在golang中设置单元测试。但是现在我在运行gotest-v时遇到错误。我想解决这个错误并使测试成功。article├client├api│├main.go│├contoroller││├contoroller.go││└contoroller_test.go│├service││├service.go││└service_test.go│├dao││├dao.go││└dao_test.go│├s3││├s3.go││└s3_test.go│├go.mod│├go.sum│└Dockerfile├nginx└docker-compose.yml现在我正在为service.go设

xml - 使用http在golang中读取FederationMetadata.xml

我正在尝试使用以下方式在golan中使用http获取FederationMetadata.xml。response,err:=client.Get("https://domainc.local/FederationMetadata/2007-06/FederationMetadata.xml").响应主体以所有数值(见下文)而非XML格式返回。但我可以从chrome下载这个文件作为xml文件。61341171141105811197115105115581109710910111558116995883657776585046485897115115101114116105111110