Beispiel #1
0
func (s *suite) TestGetURLAsStringHTTPGetError(c *gc.C) {
	s.PatchValue(httpfetch.HTTPGet, func(u string) (*http.Response, error) {
		return nil, errors.New("crash and burn")
	})
	got, err := httpfetch.GetURLAsString("http://0.1.2.3/")
	c.Assert(err, gc.ErrorMatches, "GET failed: crash and burn")
	c.Assert(got, gc.Equals, "")
}
Beispiel #2
0
func (s *suite) TestGetURLAsStringNotFound(c *gc.C) {
	srv := httptest.NewServer(http.HandlerFunc(http.NotFound))
	defer srv.Close()

	got, err := httpfetch.GetURLAsString(srv.URL)
	c.Assert(err, gc.ErrorMatches, `GET returned unexpected status "404 Not Found"`)
	c.Assert(got, gc.Equals, "")
}
Beispiel #3
0
func TestGetURLAsStringNotFound(t *testing.T) {
	srv := httptest.NewServer(http.HandlerFunc(http.NotFound))
	defer srv.Close()

	got, err := httpfetch.GetURLAsString(srv.URL)
	if err == nil {
		t.Fatalf("unexpected success with result %q", got)
	}
}
Beispiel #4
0
func (s *suite) TestGetURLAsStringReadBodyError(c *gc.C) {
	body := ioutil.NopCloser(errorReader("an error"))
	s.PatchValue(httpfetch.HTTPGet, func(u string) (*http.Response, error) {
		return &http.Response{
			Body:       body,
			StatusCode: http.StatusOK,
		}, nil
	})
	got, err := httpfetch.GetURLAsString("http://0.1.2.3/")
	c.Assert(err, gc.ErrorMatches, "cannot read body: an error")
	c.Assert(got, gc.Equals, "")
}
Beispiel #5
0
func (s *suite) TestGetURLAsStringSuccess(c *gc.C) {
	text := "hello, world\n"
	handler := func(w http.ResponseWriter, req *http.Request) {
		w.Write([]byte(text))
	}
	srv := httptest.NewServer(http.HandlerFunc(handler))
	defer srv.Close()

	got, err := httpfetch.GetURLAsString(srv.URL)
	c.Assert(err, gc.Equals, nil)
	c.Assert(got, gc.Equals, text)
}
Beispiel #6
0
func (s *suite) TestGetURLAsStringClosesBody(c *gc.C) {
	body := &closeChecker{
		Reader: strings.NewReader("hello"),
	}
	s.PatchValue(httpfetch.HTTPGet, func(u string) (*http.Response, error) {
		return &http.Response{
			Body:       body,
			StatusCode: http.StatusOK,
		}, nil
	})
	got, err := httpfetch.GetURLAsString("http://0.1.2.3/")
	c.Assert(err, gc.IsNil)
	c.Assert(got, gc.Equals, "hello")
	c.Assert(body.closed, gc.Equals, true)
}
Beispiel #7
0
func TestGetURLAsStringSuccess(t *testing.T) {
	text := "hello, world\n"
	handler := func(w http.ResponseWriter, req *http.Request) {
		w.Write([]byte(text))
	}
	srv := httptest.NewServer(http.HandlerFunc(handler))
	defer srv.Close()

	got, err := httpfetch.GetURLAsString(srv.URL)
	if err != nil {
		t.Fatalf("GetURLAsString error: %v", err)
	}
	if got != text {
		t.Fatalf("GetURLAsString returned invalid text; got %q want %q", got, text)
	}
}