Skip to content

Commit

Permalink
bitbucketserver: Ignore host when replaying API fixtures (sourcegraph…
Browse files Browse the repository at this point in the history
…#5632)

* bitbucketserver: Ignore host when replaying API fixtures

On CI we now set BITBUCKET_SERVER_HOST, which has lead to CI failing since we
now attempt to speak to the host. We speak to the host since the URLs now don't
match what is recorded in our fixtures. This commit updates the matching logic
so that we ignore the host and scheme, and instead only rely on the Path for
matching a request to a fixture.

* TestProvider_Validate does not depend on BITBUCKET_SERVER_URL
  • Loading branch information
keegancsmith authored and Loïc Guychard committed Sep 19, 2019
1 parent 3aba8fa commit 183ce8b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"reflect"
"sort"
"strconv"
"strings"
"testing"
"time"

Expand All @@ -24,6 +25,11 @@ import (
var update = flag.Bool("update", false, "update testdata")

func TestProvider_Validate(t *testing.T) {
instanceURL := os.Getenv("BITBUCKET_SERVER_URL")
if instanceURL == "" {
instanceURL = "https://127.0.0.1:7990"
}

for _, tc := range []struct {
name string
client func(*bitbucketserver.Client)
Expand All @@ -36,7 +42,7 @@ func TestProvider_Validate(t *testing.T) {
name: "problems-when-authenticated-as-non-admin",
client: func(c *bitbucketserver.Client) { c.Oauth = nil },
problems: []string{
`Bitbucket API HTTP error: code=401 url="https://127.0.0.1:7990/rest/api/1.0/admin/permissions/users?filter=" body="{\"errors\":[{\"context\":null,\"message\":\"You are not permitted to access this resource\",\"exceptionName\":\"com.atlassian.bitbucket.AuthorisationException\"}]}"`,
`Bitbucket API HTTP error: code=401 url="${INSTANCEURL}/rest/api/1.0/admin/permissions/users?filter=" body="{\"errors\":[{\"context\":null,\"message\":\"You are not permitted to access this resource\",\"exceptionName\":\"com.atlassian.bitbucket.AuthorisationException\"}]}"`,
},
},
} {
Expand All @@ -50,6 +56,10 @@ func TestProvider_Validate(t *testing.T) {
tc.client(p.client)
}

for i := range tc.problems {
tc.problems[i] = strings.ReplaceAll(tc.problems[i], "${INSTANCEURL}", instanceURL)
}

problems := p.Validate()
if have, want := problems, tc.problems; !reflect.DeepEqual(have, want) {
t.Error(cmp.Diff(have, want))
Expand Down
16 changes: 16 additions & 0 deletions pkg/extsvc/bitbucketserver/testing.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package bitbucketserver

import (
"net/http"
"net/url"
"os"
"path/filepath"
"regexp"
"testing"

"github.com/dnaeon/go-vcr/cassette"
"github.com/sourcegraph/sourcegraph/pkg/httpcli"
"github.com/sourcegraph/sourcegraph/pkg/httptestutil"
)
Expand All @@ -21,6 +23,7 @@ func NewTestClient(t testing.TB, name string, update bool) (*Client, func()) {
if err != nil {
t.Fatal(err)
}
rec.SetMatcher(ignoreHostMatcher)

hc, err := httpcli.NewFactory(nil, httptestutil.NewRecorderOpt(rec)).Doer()
if err != nil {
Expand Down Expand Up @@ -52,3 +55,16 @@ var normalizer = regexp.MustCompile("[^A-Za-z0-9-]+")
func normalize(path string) string {
return normalizer.ReplaceAllLiteralString(path, "-")
}

func ignoreHostMatcher(r *http.Request, i cassette.Request) bool {
if r.Method != i.Method {
return false
}
u, err := url.Parse(i.URL)
if err != nil {
return false
}
u.Host = r.URL.Host
u.Scheme = r.URL.Scheme
return r.URL.String() == u.String()
}

0 comments on commit 183ce8b

Please sign in to comment.