Skip to content

Commit

Permalink
finish client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pPrecel committed Feb 27, 2024
1 parent cc2f410 commit 19c1d6a
Showing 1 changed file with 95 additions and 102 deletions.
197 changes: 95 additions & 102 deletions internal/btp/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@ import (
)

func Test_oauthTransport_RoundTrip(t *testing.T) {

t.Parallel()

t.Run("client bearer authorization", func(t *testing.T) {
svr := httptest.NewServer(http.HandlerFunc(func(_ http.ResponseWriter, r *http.Request) {
require.Equal(t, "Bearer token", r.Header.Get("Authorization"))
Expand Down Expand Up @@ -45,69 +48,115 @@ func Test_GenericRequest(t *testing.T) {
}))
defer testErrorServer.Close()

t.Run("simple GET request", func(t *testing.T) {
c := httpClient{client: http.DefaultClient}

response, err := c.genericRequest(http.MethodGet, testEmptyServer.URL, requestOptions{})

require.NoError(t, err)
require.NotNil(t, response)
require.Equal(t, 200, response.StatusCode)

_ = response.Body.Close()
})

t.Run("simple POST request with additional data", func(t *testing.T) {
c := httpClient{client: http.DefaultClient}

response, err := c.genericRequest(http.MethodPost, testServer.URL, fixRequestOptions())

require.NoError(t, err)
require.NotNil(t, response)
require.Equal(t, 200, response.StatusCode)

_ = response.Body.Close()
})

t.Run("build request error becuse of wrong method name", func(t *testing.T) {
c := httpClient{client: http.DefaultClient}

response, err := c.genericRequest("DoEsNoTeXiSt)", testServer.URL, requestOptions{})

require.Equal(t, errors.New("failed to build request: net/http: invalid method \"DoEsNoTeXiSt)\""), err)
require.Nil(t, response)
})

t.Run("cant reach server by URL error", func(t *testing.T) {
c := httpClient{client: http.DefaultClient}

response, err := c.genericRequest(http.MethodGet, "http://does-not-exist", requestOptions{})

require.Equal(t, errors.New("failed to get data from server: Get \"http://does-not-exist\": dial tcp: lookup does-not-exist: no such host"), err)
require.Nil(t, response)
})

t.Run("handle 415 response status", func(t *testing.T) {
c := httpClient{client: http.DefaultClient}

response, err := c.genericRequest(http.MethodGet, testErrorServer.URL, requestOptions{})

require.Equal(t, errors.New("failed to parse http error for status: 415 Unsupported Media Type"), err)
require.Nil(t, response)
})
}

func Test_httpClient_buildResponseError(t *testing.T) {

t.Parallel()

tests := []struct {
name string
method string
url string
options requestOptions
testServer *httptest.Server
response *http.Response
expectedErr error
}{
{
name: "simple GET request",
method: http.MethodGet,
options: requestOptions{},

testServer: testEmptyServer,
expectedErr: nil,
},
{
name: "simple POST request with additional data",
method: http.MethodGet,
options: fixRequestOptions(),

testServer: testServer,
expectedErr: nil,
name: "build error from status",
response: &http.Response{
Status: "Unauthorized",
Body: io.NopCloser(strings.NewReader("")),
},
expectedErr: errors.New("failed to parse http error for status: Unauthorized"),
},
{
name: "build request error becuse of wrong method name",
method: "DoEsNoTeXiSt)",
options: requestOptions{},

testServer: testEmptyServer,
expectedErr: errors.New("failed to build request: net/http: invalid method \"DoEsNoTeXiSt)\""),
name: "build error from header",
response: &http.Response{
Status: "Unauthorized",
Header: http.Header{
"Www-Authenticate": []string{"Bearer error=\"error\",error_description=\"description\""},
},
Body: io.NopCloser(strings.NewReader("")),
},
expectedErr: errors.New("error: description"),
},
{
name: "call server error",
method: http.MethodGet,
options: requestOptions{},

testServer: &httptest.Server{
URL: "does-not-exist",
name: "build error from body",
response: &http.Response{
Status: "Unauthorized",
Body: io.NopCloser(strings.NewReader(`{
"error": {
"code": 123,
"message": "message",
"target": "target",
"correlationID": "correlationID"
}
}`)),
},
expectedErr: errors.New("failed to get data from server: Get \"does-not-exist\": unsupported protocol scheme \"\""),
expectedErr: errors.New("message"),
},
{
name: "handle 415 response status",
method: http.MethodGet,
options: requestOptions{},

testServer: testErrorServer,
expectedErr: errors.New("failed to parse http error for status: 415 Unsupported Media Type"),
name: "decode response error",
response: &http.Response{
Status: "Unauthorized",
Body: io.NopCloser(strings.NewReader("[test=value]")),
},
expectedErr: errors.New("failed to decode error response with status 'Unauthorized': invalid character 'e' in literal true (expecting 'r')"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := httpClient{
client: &http.Client{
Transport: &oauthTransport{
token: &XSUAAToken{AccessToken: ""},
},
},
}
c := &httpClient{}

_, err := c.genericRequest(tt.method, tt.testServer.URL, tt.options)
err := c.buildResponseError(tt.response)

require.Equal(t, tt.expectedErr, err)
})
Expand Down Expand Up @@ -156,59 +205,3 @@ func fixGenericRequestHandler(t *testing.T, expectedOptions requestOptions) func
w.WriteHeader(200)
}
}

func Test_httpClient_buildResponseError(t *testing.T) {

tests := []struct {
name string
response *http.Response
expectedErr error
}{
{},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &httpClient{}

err := c.buildResponseError(tt.response)

require.Equal(t, tt.expectedErr, err)
})
}
}

func Test_BuildErrorFromHeaders(t *testing.T) {
t.Parallel()

tests := []struct {
name string
response http.Response
expectedErr error
}{
{
name: "No header",
response: http.Response{
Status: "Unauthorized",
},
expectedErr: errors.New("failed to parse http error for status: Unauthorized"),
},
{
name: "Error with decription",
response: http.Response{
Status: "Unauthorized",
Header: http.Header{
"Www-Authenticate": []string{"Bearer error=\"error\",error_description=\"description\""},
},
},
expectedErr: errors.New("error: description"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := &httpClient{}
err := c.buildErrorFromHeaders(&tt.response)
require.Equal(t, tt.expectedErr, err)
})
}
}

0 comments on commit 19c1d6a

Please sign in to comment.