2009-06-09 11:58:58 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Tests for client.go
|
|
|
|
|
|
|
|
package http
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:35:38 -07:00
|
|
|
"io/ioutil"
|
2011-02-23 16:03:30 -07:00
|
|
|
"os"
|
2009-12-15 16:35:38 -07:00
|
|
|
"strings"
|
|
|
|
"testing"
|
2009-06-09 11:58:58 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestClient(t *testing.T) {
|
|
|
|
// TODO: add a proper test suite. Current test merely verifies that
|
2009-06-11 16:55:03 -06:00
|
|
|
// we can retrieve the Google robots.txt file.
|
|
|
|
|
2009-12-15 16:35:38 -07:00
|
|
|
r, _, err := Get("http://www.google.com/robots.txt")
|
|
|
|
var b []byte
|
2009-06-09 11:58:58 -06:00
|
|
|
if err == nil {
|
2009-12-15 16:35:38 -07:00
|
|
|
b, err = ioutil.ReadAll(r.Body)
|
|
|
|
r.Body.Close()
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
2009-06-22 14:26:13 -06:00
|
|
|
if err != nil {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Error(err)
|
2009-06-24 21:12:50 -06:00
|
|
|
} else if s := string(b); !strings.HasPrefix(s, "User-agent:") {
|
2009-11-09 13:07:39 -07:00
|
|
|
t.Errorf("Incorrect page body (did not begin with User-agent): %q", s)
|
2009-06-09 11:58:58 -06:00
|
|
|
}
|
|
|
|
}
|
2010-06-07 17:28:40 -06:00
|
|
|
|
|
|
|
func TestClientHead(t *testing.T) {
|
|
|
|
r, err := Head("http://www.google.com/robots.txt")
|
|
|
|
if err != nil {
|
2010-06-30 02:57:27 -06:00
|
|
|
t.Fatal(err)
|
2010-06-07 17:28:40 -06:00
|
|
|
}
|
|
|
|
if _, ok := r.Header["Last-Modified"]; !ok {
|
|
|
|
t.Error("Last-Modified header not found.")
|
|
|
|
}
|
|
|
|
}
|
2011-02-23 16:03:30 -07:00
|
|
|
|
|
|
|
type recordingTransport struct {
|
|
|
|
req *Request
|
|
|
|
}
|
|
|
|
|
|
|
|
func (t *recordingTransport) Do(req *Request) (resp *Response, err os.Error) {
|
|
|
|
t.req = req
|
|
|
|
return nil, os.NewError("dummy impl")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestGetRequestFormat(t *testing.T) {
|
|
|
|
tr := &recordingTransport{}
|
2011-03-01 15:07:28 -07:00
|
|
|
client := &Client{Transport: tr}
|
2011-02-23 16:03:30 -07:00
|
|
|
url := "http://dummy.faketld/"
|
|
|
|
client.Get(url) // Note: doesn't hit network
|
|
|
|
if tr.req.Method != "GET" {
|
|
|
|
t.Fatalf("expected method %q; got %q", "GET", tr.req.Method)
|
|
|
|
}
|
|
|
|
if tr.req.URL.String() != url {
|
|
|
|
t.Fatalf("expected URL %q; got %q", url, tr.req.URL.String())
|
|
|
|
}
|
|
|
|
}
|