Skip to content

Commit c779602

Browse files
committed
test(entry_certificate): fix CRUD test order and raw data parsing
1 parent 5b35b9c commit c779602

File tree

3 files changed

+35
-16
lines changed

3 files changed

+35
-16
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.DS_Store
2+
.vscode

dvls.go

Lines changed: 32 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ type RequestError struct {
2020
Err error
2121
}
2222

23+
const defaultContentType string = "application/json"
24+
2325
type RequestOptions struct {
2426
ContentType string
2527
RawBody bool
@@ -36,26 +38,28 @@ func (c *Client) Request(url string, reqMethod string, reqBody io.Reader, option
3638
return Response{}, &RequestError{Err: fmt.Errorf("failed to fetch login status. error: %w", err), Url: url}
3739
}
3840
if !islogged {
39-
err := c.refreshToken()
41+
err := c.login()
4042
if err != nil {
4143
return Response{}, &RequestError{Err: fmt.Errorf("failed to refresh login token. error: %w", err), Url: url}
4244
}
4345
}
4446

45-
resp, err := c.rawRequest(url, reqMethod, reqBody, options...)
47+
var opts RequestOptions
48+
if len(options) > 0 {
49+
opts = options[0]
50+
}
51+
52+
resp, err := c.rawRequest(url, reqMethod, defaultContentType, reqBody, opts)
4653
if err != nil {
4754
return Response{}, err
4855
}
4956
return resp, nil
5057
}
5158

52-
func (c *Client) rawRequest(url string, reqMethod string, reqBody io.Reader, options ...RequestOptions) (Response, error) {
53-
contentType := "application/json"
54-
var rawBody bool
55-
59+
func (c *Client) rawRequest(url string, reqMethod string, contentType string, reqBody io.Reader, options ...RequestOptions) (Response, error) {
60+
var opts RequestOptions
5661
if len(options) > 0 {
57-
contentType = options[0].ContentType
58-
rawBody = options[0].RawBody
62+
opts = options[0]
5963
}
6064

6165
req, err := http.NewRequest(reqMethod, url, reqBody)
@@ -69,7 +73,11 @@ func (c *Client) rawRequest(url string, reqMethod string, reqBody io.Reader, opt
6973
resp, err := c.client.Do(req)
7074
if err != nil {
7175
return Response{}, &RequestError{Err: fmt.Errorf("error while submitting request. error: %w", err), Url: url}
72-
} else if resp.StatusCode != http.StatusOK {
76+
}
77+
defer resp.Body.Close()
78+
79+
// Check for unexpected status codes
80+
if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated {
7381
return Response{}, &RequestError{Err: fmt.Errorf("unexpected status code %d", resp.StatusCode), Url: url}
7482
}
7583

@@ -78,13 +86,22 @@ func (c *Client) rawRequest(url string, reqMethod string, reqBody io.Reader, opt
7886
if err != nil {
7987
return Response{}, &RequestError{Err: fmt.Errorf("failed to read response body. error: %w", err), Url: url}
8088
}
81-
defer resp.Body.Close()
8289

83-
if !rawBody {
84-
err = json.Unmarshal(response.Response, &response)
85-
if err != nil {
86-
return response, &RequestError{Err: fmt.Errorf("failed to unmarshal response body. error: %w", err), Url: url}
87-
}
90+
// If RawBody is true, return the raw response without further processing
91+
if opts.RawBody {
92+
return response, nil
93+
}
94+
95+
// Handle empty response bodies for successful requests
96+
if len(response.Response) == 0 {
97+
response.Message = "Empty response (success)"
98+
return response, nil
99+
}
100+
101+
// Otherwise, unmarshal the response as JSON
102+
err = json.Unmarshal(response.Response, &response)
103+
if err != nil {
104+
return response, &RequestError{Err: fmt.Errorf("failed to unmarshal response body. error: %w", err), Url: url}
88105
}
89106

90107
return response, nil

entry_certificate_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ func Test_EntryCertificate(t *testing.T) {
3434
expiration := time.Date(2099, 1, 1, 0, 0, 0, 0, location)
3535
testCertificateEntry.Expiration = expiration
3636

37-
t.Run("GetEntry", test_GetCertificateEntry)
3837
t.Run("NewCertificateFile", test_NewCertificateEntryFile)
3938
t.Run("NewCertificateURL", test_NewCertificateEntryURL)
39+
t.Run("GetEntry", test_GetCertificateEntry)
4040
t.Run("UpdateEntry", test_UpdateCertificateEntry)
4141
t.Run("DeleteEntry", test_DeleteCertificateEntry)
4242
}
@@ -150,6 +150,7 @@ func test_NewCertificateEntryURL(t *testing.T) {
150150
if !reflect.DeepEqual(entry, newEntry) {
151151
t.Fatalf("fetched entry did not match test entry. Expected %#v, got %#v", entry, newEntry)
152152
}
153+
testCertificateEntry = entry
153154
}
154155

155156
func test_UpdateCertificateEntry(t *testing.T) {

0 commit comments

Comments
 (0)