forked from hashicorp/go-tfe
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequest_test.go
126 lines (102 loc) · 3.42 KB
/
request_test.go
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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfe
import (
"context"
"encoding/json"
"fmt"
"net/http"
"net/http/httptest"
"strconv"
"strings"
"testing"
retryablehttp "github.com/hashicorp/go-retryablehttp"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type fixtureBody struct {
ID string `json:"id"`
Name string `json:"name"`
Method string `json:"method"`
}
func newTestRequest(r *retryablehttp.Request) ClientRequest {
header := make(http.Header)
header.Add("TestHeader", "test-header-value")
return ClientRequest{
retryableRequest: r,
http: retryablehttp.NewClient(),
Header: header,
}
}
func TestClientRequest_DoJSON(t *testing.T) {
testServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fakeBody := map[string]any{
"id": "example",
"name": "fixture",
"method": r.Method,
}
fakeBodyRaw, err := json.Marshal(fakeBody)
require.NoError(t, err)
if strings.HasSuffix(r.URL.String(), "/ok_request") {
w.Header().Set("content-type", "application/json")
w.Header().Set("content-length", strconv.FormatInt(int64(len(fakeBodyRaw)), 10))
w.WriteHeader(http.StatusOK)
_, err = w.Write(fakeBodyRaw)
require.NoError(t, err)
} else if strings.HasSuffix(r.URL.String(), "/bad_request") {
w.WriteHeader(http.StatusBadRequest)
} else if strings.HasSuffix(r.URL.String(), "/created_request") {
w.WriteHeader(http.StatusCreated)
} else if strings.HasSuffix(r.URL.String(), "/not_modified_request") {
w.WriteHeader(http.StatusNotModified)
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
t.Cleanup(func() {
testServer.Close()
})
t.Run("Success 200 responses", func(t *testing.T) {
r, err := retryablehttp.NewRequest("PUT", fmt.Sprintf("%s/ok_request", testServer.URL), nil)
require.NoError(t, err)
ctx := context.Background()
request := newTestRequest(r)
putResponseBody := &fixtureBody{}
err = request.DoJSON(ctx, putResponseBody)
require.NoError(t, err)
assert.Equal(t, "example", putResponseBody.ID)
assert.Equal(t, "fixture", putResponseBody.Name)
assert.Equal(t, "PUT", putResponseBody.Method)
})
t.Run("Success response with no body", func(t *testing.T) {
r, err := retryablehttp.NewRequest("POST", fmt.Sprintf("%s/created_request", testServer.URL), nil)
require.NoError(t, err)
ctx := context.Background()
request := newTestRequest(r)
err = request.DoJSON(ctx, nil)
require.NoError(t, err)
})
t.Run("Not Modified response", func(t *testing.T) {
r, err := retryablehttp.NewRequest("POST", fmt.Sprintf("%s/not_modified_request", testServer.URL), nil)
require.NoError(t, err)
ctx := context.Background()
request := newTestRequest(r)
postResponseBody := &fixtureBody{}
err = request.DoJSON(ctx, postResponseBody)
require.NoError(t, err)
assert.Empty(t, postResponseBody.Method)
assert.Empty(t, postResponseBody.ID)
})
t.Run("Bad 400 responses", func(t *testing.T) {
r, err := retryablehttp.NewRequest("POST", fmt.Sprintf("%s/bad_request", testServer.URL), nil)
require.NoError(t, err)
ctx := context.Background()
request := newTestRequest(r)
postResponseBody := &fixtureBody{}
err = request.DoJSON(ctx, postResponseBody)
// body is empty (no response)
assert.Empty(t, postResponseBody.Method)
assert.Empty(t, postResponseBody.ID)
assert.EqualError(t, err, "error HTTP response: 400")
})
}