戈朗 : Testing API using httptest returns 404

标签 go http-status-code-404

我正在尝试测试我编写的与外部 API 对话的库。我想出了这段代码:

import (
    "fmt"
    "net/http"
    "net/http/httptest"
    "net/url"
    "testing"
)

var (
    // mux is the HTTP request multiplexer used with the test server.
    mux *http.ServeMux

    // client is the GitHub client being tested.
    client *Client

    // server is a test HTTP server used to provide mock API responses.
    server *httptest.Server
)

func setup() {
    mux = http.NewServeMux()
    server = httptest.NewServer(mux)

    client = NewClient(nil, "foo")
    url, _ := url.Parse(server.URL)
    client.BaseURL = url

}

func teardown() {
    server.Close()
}

func testMethod(t *testing.T, r *http.Request, want string) {
    if got := r.Method; got != want {
        t.Errorf("Request method: %v, want %v", got, want)
    }
}

func TestSearchForInterest(t *testing.T) {
    setup()
    defer teardown()

    mux.HandleFunc("/topic/search?search-query=Clojure", func(w http.ResponseWriter, r *http.Request) {
        fmt.Fprintln(w, `{"results": [ {"topic": "Clojure", "id": 1000} ]}`)
    })

    results, _, err := client.Topics.SearchForInterest("Clojure")
    if err != nil {
        t.Errorf("SearchForInterest returned error: %v", err)
    }

    fmt.Println(results)

}

当我运行“go test”时,我不断收到错误 404,我不确定自己做错了什么。任何指针

最佳答案

删除查询参数。那不是一条路线。

关于戈朗 : Testing API using httptest returns 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28797765/

相关文章:

apache - 自定义错误页面 404

eclipse - Tomcat 在 Eclipse 中启动但无法连接到 http ://localhost:8085/

go - 在 Travis 上使用更新的 Go 构建 CGO

go - 我可以添加带有 Go 标志的命令吗?

ssh - 在没有 SSH 的情况下安装 Hugo Web 引擎

docker - 在 Docker 中设置 Go Glide

go - 如何通过golang做代理

laravel - Vue 路由器历史模式结合 laravel catch all 重定向 404 header 给出 200 header 状态码

ruby-on-rails - Rails 中的 404 静态错误页面,公共(public)资源的路径上下文根据请求 URI 变化

c# - IIS 显示 404 但文件存在