aboutsummaryrefslogtreecommitdiffstats
path: root/clients/http/common_test.go
blob: 287897d6b3134753a5db0b7e10b00c5fc5f36348 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package http

import (
	"net/http"
	"testing"

	. "gopkg.in/check.v1"
)

func Test(t *testing.T) { TestingT(t) }

type SuiteCommon struct{}

var _ = Suite(&SuiteCommon{})

func (s *SuiteCommon) TestNewBasicAuth(c *C) {
	a := NewBasicAuth("foo", "qux")

	c.Assert(a.Name(), Equals, "http-basic-auth")
	c.Assert(a.String(), Equals, "http-basic-auth - foo:*******")
}

func (s *SuiteCommon) TestNewHTTPError200(c *C) {
	res := &http.Response{StatusCode: 200}
	res.StatusCode = 200
	err := NewHTTPError(res)
	c.Assert(err, IsNil)
}

func (s *SuiteCommon) TestNewHTTPError401(c *C) {
	s.testNewHTTPError(c, 401, "authorization required")
}

func (s *SuiteCommon) TestNewHTTPError404(c *C) {
	s.testNewHTTPError(c, 404, "repository not found")
}

func (s *SuiteCommon) TestNewHTTPError40x(c *C) {
	s.testNewHTTPError(c, 402, "unexpected client error.*")
}

func (s *SuiteCommon) testNewHTTPError(c *C, code int, msg string) {
	req, _ := http.NewRequest("GET", "foo", nil)
	res := &http.Response{
		StatusCode: code,
		Request:    req,
	}

	err := NewHTTPError(res)
	c.Assert(err, NotNil)
	c.Assert(err, ErrorMatches, msg)
}