英文:
Golang httptest server circular dependency
问题
我想为一个函数编写一个测试,该函数执行以下操作:
- 发送一个 Get 请求到 url1,该请求会获取到 url2。
- 发送一个 Get 请求到 url2,并返回结果。
但是我不确定如何模拟 url2 的返回值,因为在服务器启动之前我无法获取到 server.URL。但是在服务器启动之后,我又无法更改处理程序。例如,运行下面的代码会出现错误 Get /url2: unsupported protocol scheme ""
。
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
)
func myFunc(client *http.Client, url1 string) string {
url2 := get(client, url1)
return get(client, url2)
}
func get(client *http.Client, url string) string {
resp, err := client.Get(url)
if err != nil {
fmt.Println(err)
}
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
fmt.Println(err)
}
return string(body)
}
// 测试 myFunc
func main() {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.String() {
case "/url1":
w.Write([]byte("/url2")) // 在这里如何指定 srv.URL+"/url2"?
case "/url2":
w.Write([]byte("返回数据"))
}
}))
defer srv.Close()
myFunc(srv.Client(), srv.URL+"/url1")
}
来源:https://onlinegdb.com/UpKlXfw45
英文:
I'd like to write a test for a function that
- Makes a Get request to url1, which retrieves url2
- Makes a Get request to url2, and returns the result
But I'm not sure how to mock the return value for url2, since I cannot fetch the server.URL before the server is started. But after the server is started I cannot change the handler. Eg, running the below gives error Get /url2: unsupported protocol scheme ""
package main
import (
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
)
func myFunc(client *http.Client, url1 string) string {
url2 := get(client, url1)
return get(client, url2)
}
func get(client *http.Client, url string) string {
resp, err := client.Get(url)
if err != nil {
fmt.Println(err)
}
body, err := ioutil.ReadAll(resp.Body)
defer resp.Body.Close()
if err != nil {
fmt.Println(err)
}
return string(body)
}
// test myFunc
func main() {
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.String() {
case "/url1":
w.Write([]byte("/url2")) // how to specify srv.URL+"/url2" here?
case "/url2":
w.Write([]byte("return data"))
}
}))
defer srv.Close()
myFunc(srv.Client(), srv.URL+"/url1")
}
Source: https://onlinegdb.com/UpKlXfw45
答案1
得分: 2
在使用变量之前声明srv变量。
var srv *httptest.Server
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.String() {
case "/url1":
w.Write([]byte(srv.URL + "/url2"))
case "/url2":
w.Write([]byte("return data"))
}
}))
英文:
Declare the srv variable before using the variable.
var srv *httptest.Server
srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.String() {
case "/url1":
w.Write([]byte(srv.URL + "/url2"))
case "/url2":
w.Write([]byte("return data"))
}
}))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论