Skip to content

Commit 24cc06c

Browse files
committed
test(entry_certificate): fix CRUD test order and raw data parsing
1 parent 5d0a777 commit 24cc06c

File tree

3 files changed

+25
-8
lines changed

3 files changed

+25
-8
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

entry_certificate.go

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"encoding/json"
66
"fmt"
7+
"io"
78
"net/http"
89
"net/url"
910
"strings"
@@ -165,15 +166,32 @@ func (c *EntryCertificateService) Get(entryId string) (EntryCertificate, error)
165166
func (c *EntryCertificateService) GetFileContent(entryId string) ([]byte, error) {
166167
reqUrl, err := url.JoinPath(c.client.baseUri, entryConnectionsEndpoint, entryId, "document")
167168
if err != nil {
168-
return nil, fmt.Errorf("failed to build entry url. error: %w", err)
169+
return nil, fmt.Errorf("failed to build entry url: %w", err)
169170
}
170171

171-
resp, err := c.client.Request(reqUrl, http.MethodGet, nil, RequestOptions{RawBody: true})
172+
req, err := http.NewRequest(http.MethodGet, reqUrl, nil)
172173
if err != nil {
173-
return nil, fmt.Errorf("error while fetching entry content. error: %w", err)
174+
return nil, fmt.Errorf("failed to create request: %w", err)
174175
}
175176

176-
return resp.Response, nil
177+
req.Header.Add("tokenId", c.client.credential.token)
178+
179+
httpResp, err := c.client.client.Do(req)
180+
if err != nil {
181+
return nil, fmt.Errorf("error while fetching entry content: %w", err)
182+
}
183+
defer httpResp.Body.Close()
184+
185+
if httpResp.StatusCode < 200 || httpResp.StatusCode >= 300 {
186+
return nil, fmt.Errorf("unexpected status code %d when fetching file content", httpResp.StatusCode)
187+
}
188+
189+
fileContent, err := io.ReadAll(httpResp.Body)
190+
if err != nil {
191+
return nil, fmt.Errorf("failed to read response body: %w", err)
192+
}
193+
194+
return fileContent, nil
177195
}
178196

179197
// GetPassword returns the password of the entry specified by entry.

entry_certificate_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package dvls
22

33
import (
4-
"fmt"
54
"io"
65
"os"
76
"reflect"
@@ -35,9 +34,9 @@ func Test_EntryCertificate(t *testing.T) {
3534
expiration := time.Date(2099, 1, 1, 0, 0, 0, 0, location)
3635
testCertificateEntry.Expiration = expiration
3736

38-
t.Run("GetEntry", test_GetCertificateEntry)
3937
t.Run("NewCertificateFile", test_NewCertificateEntryFile)
4038
t.Run("NewCertificateURL", test_NewCertificateEntryURL)
39+
t.Run("GetEntry", test_GetCertificateEntry)
4140
t.Run("UpdateEntry", test_UpdateCertificateEntry)
4241
t.Run("DeleteEntry", test_DeleteCertificateEntry)
4342
}
@@ -151,6 +150,7 @@ func test_NewCertificateEntryURL(t *testing.T) {
151150
if !reflect.DeepEqual(entry, newEntry) {
152151
t.Fatalf("fetched entry did not match test entry. Expected %#v, got %#v", entry, newEntry)
153152
}
153+
testCertificateEntry = entry
154154
}
155155

156156
func test_UpdateCertificateEntry(t *testing.T) {
@@ -177,8 +177,6 @@ func test_UpdateCertificateEntry(t *testing.T) {
177177
}
178178

179179
func test_DeleteCertificateEntry(t *testing.T) {
180-
fmt.Println(testNewCertificateEntryURL.ID)
181-
fmt.Println(testNewCertificateEntryFile.ID)
182180
err := testClient.Entries.Certificate.Delete(testNewCertificateEntryURL.ID)
183181
if err != nil {
184182
t.Fatal(err)

0 commit comments

Comments
 (0)