MCPcopy Index your code
hub / github.com/coder/coder / newGitLabVCR

Function newGitLabVCR

coderd/externalauth/gitprovider/gitlab_integration_test.go:22–71  ·  view source on GitHub ↗

newGitLabVCR creates a go-vcr recorder for GitLab integration tests. In replay mode (default), it serves responses from the cassette file. When GITLAB_UPDATE_GOLDEN=true, it records live responses to the cassette.

(t *testing.T, cassetteName string)

Source from the content-addressed store, hash-verified

20// In replay mode (default), it serves responses from the cassette file.
21// When GITLAB_UPDATE_GOLDEN=true, it records live responses to the cassette.
22func newGitLabVCR(t *testing.T, cassetteName string) *recorder.Recorder {
23 t.Helper()
24
25 mode := recorder.ModeReplayOnly
26 if update, _ := strconv.ParseBool(os.Getenv("GITLAB_UPDATE_GOLDEN")); update {
27 mode = recorder.ModeRecordOnly
28 }
29
30 rec, err := recorder.New(
31 "testdata/gitlab_cassettes/"+cassetteName,
32 recorder.WithMode(mode),
33 recorder.WithSkipRequestLatency(true),
34 // Match only on method + URL; the default matcher is too strict
35 // (compares proto, all headers, etc.) and breaks replay.
36 // TODO: consider verifying that an Authorization header is present
37 // during replay to catch auth-wiring regressions.
38 recorder.WithMatcher(func(r *http.Request, i cassette.Request) bool {
39 return r.Method == i.Method && r.URL.String() == i.URL
40 }),
41 // Strip headers down to an allowlist to reduce cassette noise.
42 recorder.WithHook(func(i *cassette.Interaction) error {
43 allowedRequestHeaders := map[string]struct{}{
44 "Accept": {},
45 "Content-Type": {},
46 }
47 for h := range i.Request.Headers {
48 if _, ok := allowedRequestHeaders[h]; !ok {
49 i.Request.Headers[h] = []string{"stripped"}
50 }
51 }
52
53 allowedResponseHeaders := map[string]struct{}{
54 "Content-Type": {},
55 "X-Total": {},
56 }
57 for h := range i.Response.Headers {
58 if _, ok := allowedResponseHeaders[h]; !ok {
59 i.Response.Headers[h] = []string{"stripped"}
60 }
61 }
62 return nil
63 }, recorder.AfterCaptureHook),
64 )
65 require.NoError(t, err)
66 t.Cleanup(func() {
67 require.NoError(t, rec.Stop())
68 })
69
70 return rec
71}
72
73// TestGitLabIntegration exercises every gitprovider.Provider method
74// against recorded GitLab API responses (go-vcr cassettes).

Callers 1

TestGitLabIntegrationFunction · 0.85

Calls 5

HelperMethod · 0.65
NewMethod · 0.65
CleanupMethod · 0.65
StopMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected