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

Function Test_Client

codersdk/client_internal_test.go:86–162  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

84}
85
86func Test_Client(t *testing.T) {
87 t.Parallel()
88
89 const method = http.MethodPost
90 const path = "/ok"
91 const token = "token"
92 const reqBody = `{"msg": "request body"}`
93 const resBody = `{"status": "ok"}`
94
95 s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
96 assert.Equal(t, method, r.Method)
97 assert.Equal(t, path, r.URL.Path)
98 assert.Equal(t, token, r.Header.Get(SessionTokenHeader))
99 assert.NotEmpty(t, r.Header.Get("Traceparent"))
100 for k, v := range r.Header {
101 t.Logf("header %q: %q", k, strings.Join(v, ", "))
102 }
103
104 w.Header().Set("Content-Type", jsonCT)
105 w.WriteHeader(http.StatusOK)
106 _, _ = io.WriteString(w, resBody)
107 }))
108
109 u, err := url.Parse(s.URL)
110 require.NoError(t, err)
111 client := New(u)
112 client.SetSessionToken(token)
113
114 logBuf := bytes.NewBuffer(nil)
115 client.SetLogger(slog.Make(sloghuman.Sink(logBuf)).Leveled(slog.LevelDebug))
116 client.SetLogBodies(true)
117
118 // Setup tracing.
119 res := resource.NewWithAttributes(
120 semconv.SchemaURL,
121 semconv.ServiceNameKey.String("codersdk_test"),
122 )
123 tracerOpts := []sdktrace.TracerProviderOption{
124 sdktrace.WithResource(res),
125 }
126 tracerProvider := sdktrace.NewTracerProvider(tracerOpts...)
127 otel.SetTracerProvider(tracerProvider)
128 otel.SetErrorHandler(otel.ErrorHandlerFunc(func(err error) {}))
129 otel.SetTextMapPropagator(
130 propagation.NewCompositeTextMapPropagator(
131 propagation.TraceContext{},
132 propagation.Baggage{},
133 ),
134 )
135 otel.SetLogger(logr.Discard())
136 client.Trace = true
137
138 ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
139 defer cancel()
140 ctx, span := tracerProvider.Tracer("codersdk_test").Start(ctx, "codersdk client test 1")
141 defer span.End()
142
143 resp, err := client.Request(ctx, method, path, []byte(reqBody))

Callers

nothing calls this directly

Calls 15

NotEmptyMethod · 0.80
WriteStringMethod · 0.80
SetSessionTokenMethod · 0.80
TracerMethod · 0.80
NewFunction · 0.70
GetMethod · 0.65
LogfMethod · 0.65
SetMethod · 0.65
ParseMethod · 0.65
SetLoggerMethod · 0.65
SetLogBodiesMethod · 0.65
StartMethod · 0.65

Tested by

no test coverage detected