I'm trying to test a library I wrote that talks to an external API. I came up with this code:
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)
}
When I run "go test" I keep getting an error 404 not sure what I'm doing wrong. any pointers
Remove the query param. that isn't a route.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.