~gwacl-hackers/gwacl/trunk

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package gwacl

import (
	"errors"
	"fmt"
	. "launchpad.net/gocheck"
	"net/http"
)

type httpErrorSuite struct{}

var _ = Suite(&httpErrorSuite{})

func (suite *httpErrorSuite) TestNewHTTPErrorParsesAzureError(c *C) {
	description := "upload failed"
	status := 415
	code := "CannotUpload"
	message := "Unknown data format"
	xml := fmt.Sprintf(`<Error>
        <Code>%s</Code>
        <Message>%s</Message>
    </Error>`,
		code, message)

	httpErr := newHTTPError(status, []byte(xml), description)

	azureErr, ok := httpErr.(*AzureError)
	c.Assert(ok, Equals, true)
	c.Check(azureErr.StatusCode(), Equals, status)
	c.Check(azureErr.Code, Equals, code)
	c.Check(azureErr.Message, Equals, message)
	c.Check(httpErr, ErrorMatches, ".*"+description+".*")
	c.Check(httpErr, ErrorMatches, ".*415: Unsupported Media Type.*")
}

func (suite *httpErrorSuite) TestNewHTTPErrorResortsToServerError(c *C) {
	description := "could not talk to server"
	status := 505

	httpErr := newHTTPError(status, []byte{}, description)

	_, ok := httpErr.(*ServerError)
	c.Assert(ok, Equals, true)
	c.Check(httpErr.StatusCode(), Equals, status)
	c.Check(httpErr, ErrorMatches, ".*505: HTTP Version Not Supported.*")
	c.Check(httpErr, ErrorMatches, ".*"+description+".*")
}

func (suite *httpErrorSuite) TestAzureErrorComposesError(c *C) {
	description := "something failed"
	status := 410
	httpErr := AzureError{
		error:      errors.New(description),
		HTTPStatus: HTTPStatus(status),
		Code:       "MissingError",
		Message:    "Your object has disappeared",
	}
	c.Check(httpErr.Error(), Equals, "something failed: MissingError - Your object has disappeared (http code 410: Gone)")
}

func (suite *httpErrorSuite) TestServerErrorComposesError(c *C) {
	description := "something failed"
	status := 501
	httpErr := ServerError{
		error:      errors.New(description),
		HTTPStatus: HTTPStatus(status),
	}
	c.Check(httpErr.Error(), Equals, "something failed (501: Not Implemented)")
}

func (suite *httpErrorSuite) TestNewAzureErrorFromOperation(c *C) {
	status := http.StatusConflict
	code := MakeRandomString(7)
	message := MakeRandomString(20)
	// Test body copied from Azure documentation, mutatis mutandi.
	body := fmt.Sprintf(`
        <?xml version="1.0" encoding="utf-8"?>
          <Operation xmlns="http://schemas.microsoft.com/windowsazure">
            <ID>%s</ID>
            <Status>Failed</Status>
            <HttpStatusCode>%d</HttpStatusCode>
            <Error>
              <Code>%s</Code>
              <Message>%s</Message>
            </Error>
          </Operation>
        `,
		MakeRandomString(5), status, code, message)
	operation := Operation{}
	operation.Deserialize([]byte(body))

	err := newAzureErrorFromOperation(&operation)
	c.Check(err.HTTPStatus, Equals, HTTPStatus(status))
	c.Check(err.Code, Equals, code)
	c.Check(err.Message, Equals, message)
}

func (suite *httpErrorSuite) TestExtendErrorExtendsGenericError(c *C) {
	errorString := "an-error"
	error := fmt.Errorf(errorString)
	additionalErrorMsg := "additional message"
	newError := extendError(error, additionalErrorMsg)
	c.Check(newError.Error(), Equals, fmt.Sprintf("%s%s", additionalErrorMsg, error.Error()))
}

func (suite *httpErrorSuite) TestExtendErrorExtendsServerError(c *C) {
	err := &ServerError{
		error:      errors.New("could not talk to server"),
		HTTPStatus: HTTPStatus(http.StatusGatewayTimeout),
	}
	additionalErrorMsg := "additional message: "
	newError := extendError(err, additionalErrorMsg)
	newServerError, ok := newError.(*ServerError)
	c.Assert(ok, Equals, true)
	c.Check(newError.Error(), Equals, additionalErrorMsg+err.Error())
	c.Check(newServerError.HTTPStatus, Equals, err.HTTPStatus)
}

func (suite *httpErrorSuite) TestExtendErrorExtendsAzureError(c *C) {
	err := &AzureError{
		error:      errors.New("could not talk to server"),
		HTTPStatus: HTTPStatus(http.StatusGatewayTimeout),
	}
	additionalErrorMsg := "additional message: "
	newError := extendError(err, additionalErrorMsg)
	newAzureError, ok := newError.(*AzureError)
	c.Assert(ok, Equals, true)
	c.Check(newError.Error(), Equals, additionalErrorMsg+err.Error())
	c.Check(newAzureError.HTTPStatus, Equals, err.HTTPStatus)
}

func (suite *httpErrorSuite) TestIsNotFound(c *C) {
	var testValues = []struct {
		err            error
		expectedResult bool
	}{
		{fmt.Errorf("generic error"), false},
		{&AzureError{HTTPStatus: HTTPStatus(http.StatusOK)}, false},
		{&AzureError{HTTPStatus: HTTPStatus(http.StatusNotFound)}, true},
		{&AzureError{HTTPStatus: HTTPStatus(http.StatusInternalServerError)}, false},
		{&ServerError{HTTPStatus: HTTPStatus(http.StatusOK)}, false},
		{&ServerError{HTTPStatus: HTTPStatus(http.StatusNotFound)}, true},
		{&ServerError{HTTPStatus: HTTPStatus(http.StatusInternalServerError)}, false},
	}
	for _, test := range testValues {
		c.Check(IsNotFoundError(test.err), Equals, test.expectedResult)
	}
}