С net/http/httptest как може да се пренасочва request? Опитах да напиша нещо такова:
package gotumblr
import (
"net/http"
"net/http/httptest"
"net/url"
"testing"
"reflect"
"fmt"
)
var (
//mux is the HTTP request multiplexer used with the test server.
mux *http.ServeMux
//client is the Tublr client being tested.
client *TumblrRestClient
//server is a test HTTP server used to provide mock API responses.
server *httptest.Server
)
//setup sets up a test HTTP server along with a gotumblr.TumblrRestClient that is
// configured to talk to that server. Tests should register handlers on
// mux which provide mock responses for the API method being tested.
func setup() {
//test server
mux = http.NewServeMux()
server = httptest.NewServer(mux)
//tumblr client configured to use test server
host, _ := url.Parse(server.URL)
client = NewTumblrRestClient("", "", "", "", "", host.String())
}
//teardown closes the test HTTP server.
func teardown() {
server.Close()
}
func TestAvatar(t *testing.T) {
setup()
defer teardown()
mux.HandleFunc("/v2/blog/mgterzieva/avatar/64", func(w http.ResponseWriter, r *http.Request) {
http.Redirect(w, r, "/picture/found/here", http.StatusFound)
})
mux.HandleFunc("/picture/found/here", func(w http.ResponseWriter, r *http.Request) {
if m := "GET"; m != r.Method {
t.Errorf("Request method = %v, want %v", r.Method, m)
}
fmt.Fprint(w, `{"response": {"avatar_url": "http://cool-pics.jpg"}}`)
})
avatar := client.Avatar("mgterzieva", 64).Avatar_url
want := "http://cool-pics.jpg"
if !reflect.DeepEqual(avatar, want) {
t.Errorf("Avatar returned %+v, want %+v", avatar, want)
}
}
но получавам Avatar returned , want http://cool-pics.jpg