Skip to content

Commit

Permalink
fix: workspace pact test
Browse files Browse the repository at this point in the history
  • Loading branch information
bastiandoetsch committed Jun 5, 2024
1 parent 294cddd commit 9b2c692
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 50 deletions.
1 change: 1 addition & 0 deletions http/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions internal/deepcode/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
16 changes: 10 additions & 6 deletions internal/deepcode/client_pact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
}
Expand All @@ -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()

Expand All @@ -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)),
Expand Down Expand Up @@ -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
}
Expand Down
37 changes: 19 additions & 18 deletions internal/deepcode/pacts/code-client-go-snykcodeapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
6 changes: 4 additions & 2 deletions internal/workspace/2024-05-14/client_pact_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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),
Expand All @@ -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"),
}
}

Expand All @@ -159,5 +161,5 @@ func getSnykRequestIdMatcher() matchers.Matcher {
}

func getBodyMatcher() matchers.Matcher {
return matchers.Like(make([]byte, 1))
return matchers.MatchV2(v20240514.CreateWorkspaceApplicationVndAPIPlusJSONRequestBody{})
}
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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"
},
Expand All @@ -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"
}
}

0 comments on commit 9b2c692

Please sign in to comment.