MCPcopy
hub / github.com/github/github-mcp-server / TestMiddlewareScenario

Function TestMiddlewareScenario

pkg/errors/error_test.go:391–463  ·  view source on GitHub ↗

TestMiddlewareScenario demonstrates a realistic middleware scenario

(t *testing.T)

Source from the content-addressed store, hash-verified

389
390// TestMiddlewareScenario demonstrates a realistic middleware scenario
391func TestMiddlewareScenario(t *testing.T) {
392 t.Run("realistic middleware error collection scenario", func(t *testing.T) {
393 // Simulate a realistic HTTP middleware scenario
394
395 // 1. Request comes in, middleware sets up error tracking
396 ctx := ContextWithGitHubErrors(context.Background())
397
398 // 2. Middleware stores reference to context for later inspection
399 var middlewareCtx context.Context
400 setupMiddleware := func(ctx context.Context) context.Context {
401 middlewareCtx = ctx
402 return ctx
403 }
404
405 // 3. Setup middleware
406 ctx = setupMiddleware(ctx)
407
408 // 4. Simulate multiple service calls that add errors
409 simulateServiceCall1 := func(ctx context.Context) {
410 resp := &github.Response{Response: &http.Response{StatusCode: 403}}
411 _, err := NewGitHubAPIErrorToCtx(ctx, "insufficient permissions", resp, fmt.Errorf("forbidden"))
412 require.NoError(t, err)
413 }
414
415 simulateServiceCall2 := func(ctx context.Context) {
416 resp := &github.Response{Response: &http.Response{StatusCode: 404}}
417 _, err := NewGitHubAPIErrorToCtx(ctx, "resource not found", resp, fmt.Errorf("not found"))
418 require.NoError(t, err)
419 }
420
421 simulateGraphQLCall := func(ctx context.Context) {
422 gqlErr := newGitHubGraphQLError("mutation failed", fmt.Errorf("invalid input"))
423 _, err := addGitHubGraphQLErrorToContext(ctx, gqlErr)
424 require.NoError(t, err)
425 }
426
427 // 5. Execute service calls (without context propagation)
428 simulateServiceCall1(ctx)
429 simulateServiceCall2(ctx)
430 simulateGraphQLCall(ctx)
431
432 // 6. Middleware inspects errors at the end of request processing
433 finalizeMiddleware := func(ctx context.Context) ([]string, []string) {
434 var apiErrorMessages []string
435 var gqlErrorMessages []string
436
437 if apiErrors, err := GetGitHubAPIErrors(ctx); err == nil {
438 for _, apiErr := range apiErrors {
439 apiErrorMessages = append(apiErrorMessages, apiErr.Message)
440 }
441 }
442
443 if gqlErrors, err := GetGitHubGraphQLErrors(ctx); err == nil {
444 for _, gqlErr := range gqlErrors {
445 gqlErrorMessages = append(gqlErrorMessages, gqlErr.Message)
446 }
447 }
448

Callers

nothing calls this directly

Calls 6

ContextWithGitHubErrorsFunction · 0.85
NewGitHubAPIErrorToCtxFunction · 0.85
newGitHubGraphQLErrorFunction · 0.85
GetGitHubAPIErrorsFunction · 0.85
GetGitHubGraphQLErrorsFunction · 0.85

Tested by

no test coverage detected