diff --git a/http/http.go b/http/http.go index c7fa726..0595f3d 100644 --- a/http/http.go +++ b/http/http.go @@ -144,6 +144,7 @@ func (s *httpClient) httpCall(req *http.Request) (*http.Response, error) { req.Body = reqBody log.Debug().Msg("SEND TO REMOTE") } + response, err := s.httpClientFactory().Do(req) req.Body = copyReqBody if response != nil { diff --git a/internal/deepcode/client.go b/internal/deepcode/client.go index c948adb..6958bd8 100644 --- a/internal/deepcode/client.go +++ b/internal/deepcode/client.go @@ -22,14 +22,15 @@ import ( "encoding/json" "errors" "fmt" - "github.com/snyk/code-client-go/config" - "github.com/snyk/code-client-go/internal/util/encoding" "io" "net/http" "net/url" "regexp" "strconv" + "github.com/snyk/code-client-go/config" + "github.com/snyk/code-client-go/internal/util/encoding" + "github.com/rs/zerolog" codeClientHTTP "github.com/snyk/code-client-go/http" @@ -295,5 +296,5 @@ func (s *deepcodeClient) checkResponseCode(r *http.Response) error { if r.StatusCode >= 200 && r.StatusCode <= 299 { return nil } - return fmt.Errorf("Unexpected response code: %s", r.Status) + return fmt.Errorf("unexpected response code: %s (%s)", r.Status, r.Body) } diff --git a/internal/deepcode/client_pact_test.go b/internal/deepcode/client_pact_test.go index 19abbeb..2a670a9 100644 --- a/internal/deepcode/client_pact_test.go +++ b/internal/deepcode/client_pact_test.go @@ -69,7 +69,7 @@ func TestSnykCodeClientPact(t *testing.T) { }) test := func(config consumer.MockServerConfig) error { - client := getDeepCodeClient(t, fmt.Sprintf("%s:%d", config.Host, config.Port)) + client := getDeepCodeClient(t, getLocalMockserver(config)) files := make(map[string]string) files[path1] = util.Hash([]byte(content)) bundleHash, missingFiles, err := client.CreateBundle(context.Background(), files) @@ -111,7 +111,7 @@ func TestSnykCodeClientPact(t *testing.T) { }) test := func(config consumer.MockServerConfig) error { - client := getDeepCodeClient(t, fmt.Sprintf("%s:%d", config.Host, config.Port)) + client := getDeepCodeClient(t, getLocalMockserver(config)) files := make(map[string]string) files[path1] = util.Hash([]byte(content)) _, _, err := client.CreateBundle(context.Background(), files) @@ -147,7 +147,7 @@ func TestSnykCodeClientPact(t *testing.T) { }) test := func(config consumer.MockServerConfig) error { - client := getDeepCodeClient(t, fmt.Sprintf("%s:%d", config.Host, config.Port)) + client := getDeepCodeClient(t, getLocalMockserver(config)) filesExtend := createTestExtendMap() var removedFiles []string @@ -190,7 +190,7 @@ func TestSnykCodeClientPact(t *testing.T) { }) test := func(config consumer.MockServerConfig) error { - client := getDeepCodeClient(t, fmt.Sprintf("%s:%d", config.Host, config.Port)) + client := getDeepCodeClient(t, getLocalMockserver(config)) if _, err := client.GetFilters(context.Background()); err != nil { return err } @@ -204,6 +204,10 @@ func TestSnykCodeClientPact(t *testing.T) { }) } +func getLocalMockserver(config consumer.MockServerConfig) string { + return fmt.Sprintf("http://%s:%d", config.Host, config.Port) +} + func setupPact(t *testing.T) { t.Helper() @@ -225,7 +229,7 @@ func getDeepCodeClient(t *testing.T, snykCodeApiUrl string) deepcode.DeepcodeCli func() *http.Client { return http.DefaultClient }, - codeClientHTTP.WithRetryCount(3), + codeClientHTTP.WithRetryCount(1), codeClientHTTP.WithInstrumentor(instrumentor), codeClientHTTP.WithErrorReporter(errorReporter), codeClientHTTP.WithLogger(newLogger(t)), @@ -275,7 +279,7 @@ func TestSnykCodeClientPact_LocalCodeEngine(t *testing.T) { }) test := func(config consumer.MockServerConfig) error { - client := getDeepCodeClient(t, fmt.Sprintf("%s:%d", config.Host, config.Port)) + client := getDeepCodeClient(t, getLocalMockserver(config)) if _, err := client.GetFilters(context.Background()); err != nil { return err } diff --git a/internal/deepcode/pacts/code-client-go-snykcodeapi.json b/internal/deepcode/pacts/code-client-go-snykcodeapi.json index f7d00b0..32248f2 100644 --- a/internal/deepcode/pacts/code-client-go-snykcodeapi.json +++ b/internal/deepcode/pacts/code-client-go-snykcodeapi.json @@ -2,31 +2,24 @@ "consumer": { "name": "code-client-go" }, - "provider": { - "name": "SnykCodeApi" - }, "interactions": [ { "description": "Get filters", "request": { - "method": "GET", - "path": "/filters", "headers": { "Content-Type": "application/json", "snyk-request-id": "fc763eba-0905-41c5-a27f-3934ab26786c" }, "matchingRules": { - "$.headers.snyk-request-id": { + "$.header['snyk-request-id']": { "match": "regex", "regex": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" } - } + }, + "method": "GET", + "path": "/filters" }, "response": { - "status": 200, - "headers": { - "Content-Type": "application/json" - }, "body": { "configFiles": [ "string" @@ -35,32 +28,40 @@ "string" ] }, + "headers": { + "Content-Type": "application/json" + }, "matchingRules": { "$.body.configFiles": { + "match": "type", "min": 1 }, - "$.body.configFiles[*].*": { - "match": "type" - }, "$.body.configFiles[*]": { "match": "type" }, "$.body.extensions": { + "match": "type", "min": 1 }, - "$.body.extensions[*].*": { - "match": "type" - }, "$.body.extensions[*]": { "match": "type" } - } + }, + "status": 200 } } ], "metadata": { + "pactRust": { + "ffi": "0.4.16", + "mockserver": "1.2.5", + "models": "1.1.19" + }, "pactSpecification": { "version": "2.0.0" } + }, + "provider": { + "name": "SnykCodeApi" } } \ No newline at end of file diff --git a/internal/orchestration/2024-02-16/pacts/code-client-go-orchestrationapi.json b/internal/orchestration/2024-02-16/pacts/code-client-go-orchestrationapi.json index be83d51..61c087f 100644 --- a/internal/orchestration/2024-02-16/pacts/code-client-go-orchestrationapi.json +++ b/internal/orchestration/2024-02-16/pacts/code-client-go-orchestrationapi.json @@ -164,7 +164,7 @@ "providerState": "Scan ID", "request": { "method": "GET", - "path": "/orgs/e7ea34c9-de0f-422c-bf2c-4654c2e2da90/scans/dc0d9f32-3081-4694-8052-1f4465b4ff43", + "path": "/orgs/e7ea34c9-de0f-422c-bf2c-4654c2e2da90/scans/5be49f72-a11f-4670-9657-5f5a9c92cdd7", "query": "version=2024-02-16%7eexperimental" }, "response": { diff --git a/internal/workspace/2024-05-14/client_pact_test.go b/internal/workspace/2024-05-14/client_pact_test.go index 1857e39..1e65a6b 100644 --- a/internal/workspace/2024-05-14/client_pact_test.go +++ b/internal/workspace/2024-05-14/client_pact_test.go @@ -83,6 +83,7 @@ func TestWorkspaceClientPact(t *testing.T) { &v20240514.CreateWorkspaceParams{ Version: "2024-05-14~experimental", SnykRequestId: uuid.MustParse(requestId), + ContentType: "application/vnd.api+json", }, v20240514.CreateWorkspaceApplicationVndAPIPlusJSONRequestBody{ Data: struct { @@ -140,7 +141,7 @@ func setupPact(t *testing.T) { func() *http.Client { return http.DefaultClient }, - codeClientHTTP.WithRetryCount(3), + codeClientHTTP.WithRetryCount(1), codeClientHTTP.WithInstrumentor(instrumentor), codeClientHTTP.WithErrorReporter(errorReporter), codeClientHTTP.WithLogger(&logger), @@ -151,6 +152,7 @@ func setupPact(t *testing.T) { func getHeaderMatcher() matchers.MapMatcher { return matchers.MapMatcher{ "Snyk-Request-Id": getSnykRequestIdMatcher(), + "Content-Type": matchers.S("application/vnd.api+json"), } } @@ -159,5 +161,5 @@ func getSnykRequestIdMatcher() matchers.Matcher { } func getBodyMatcher() matchers.Matcher { - return matchers.Like(make([]byte, 1)) + return matchers.MatchV2(v20240514.CreateWorkspaceApplicationVndAPIPlusJSONRequestBody{}) } diff --git a/internal/workspace/2024-05-14/pacts/code-client-go-workspaceapi.json b/internal/workspace/2024-05-14/pacts/code-client-go-workspaceapi.json index be8213d..5b0bf3f 100644 --- a/internal/workspace/2024-05-14/pacts/code-client-go-workspaceapi.json +++ b/internal/workspace/2024-05-14/pacts/code-client-go-workspaceapi.json @@ -2,36 +2,52 @@ "consumer": { "name": "code-client-go" }, - "provider": { - "name": "WorkspaceApi" - }, "interactions": [ { "description": "Create workspace", "providerState": "New workspace", "request": { - "method": "POST", - "path": "/orgs/e7ea34c9-de0f-422c-bf2c-4654c2e2da90/workspaces", - "query": "version=2024-05-14%7Eexperimental", + "body": { + "data": { + "attributes": { + "bundle_id": "string", + "repository_uri": "string", + "root_folder_id": "string", + "workspace_type": "string" + }, + "type": "string" + } + }, "headers": { + "Content-Type": "application/vnd.api+json", "Snyk-Request-Id": "fc763eba-0905-41c5-a27f-3934ab26786c" }, - "body": "AA==", "matchingRules": { - "$.headers.Snyk-Request-Id": { - "match": "regex", - "regex": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" + "$.body.data.attributes.bundle_id": { + "match": "type" + }, + "$.body.data.attributes.repository_uri": { + "match": "type" + }, + "$.body.data.attributes.root_folder_id": { + "match": "type" + }, + "$.body.data.attributes.workspace_type": { + "match": "type" }, - "$.body": { + "$.body.data.type": { "match": "type" + }, + "$.header['Snyk-Request-Id']": { + "match": "regex", + "regex": "^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}" } - } + }, + "method": "POST", + "path": "/orgs/e7ea34c9-de0f-422c-bf2c-4654c2e2da90/workspaces", + "query": "version=2024-05-14%7eexperimental" }, "response": { - "status": 200, - "headers": { - "Content-Type": "application/vnd.api+json" - }, "body": { "data": { "id": [ @@ -48,13 +64,14 @@ } } }, + "headers": { + "Content-Type": "application/vnd.api+json" + }, "matchingRules": { "$.body.data.id": { + "match": "type", "min": 1 }, - "$.body.data.id[*].*": { - "match": "type" - }, "$.body.data.id[*]": { "match": "type" }, @@ -67,13 +84,22 @@ "$.body.links.self.href": { "match": "type" } - } + }, + "status": 200 } } ], "metadata": { + "pactRust": { + "ffi": "0.4.16", + "mockserver": "1.2.5", + "models": "1.1.19" + }, "pactSpecification": { "version": "2.0.0" } + }, + "provider": { + "name": "WorkspaceApi" } } \ No newline at end of file