(eventPayload string, failAfter int, overrides ...eventMetadata)
| 482 | } |
| 483 | |
| 484 | func runtimeAPIServer(eventPayload string, failAfter int, overrides ...eventMetadata) (*httptest.Server, *requestRecord) { |
| 485 | ctx, cancel := context.WithCancel(context.Background()) |
| 486 | numInvokesRequested := 0 |
| 487 | numInvokesRequestedLock := sync.Mutex{} |
| 488 | record := &requestRecord{} |
| 489 | |
| 490 | ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 491 | switch r.Method { |
| 492 | case http.MethodGet: |
| 493 | numInvokesRequestedLock.Lock() |
| 494 | metadata := defaultInvokeMetadata() |
| 495 | if numInvokesRequested < len(overrides) { |
| 496 | metadata = overrides[numInvokesRequested] |
| 497 | } |
| 498 | numInvokesRequested++ |
| 499 | shouldFail := numInvokesRequested > failAfter |
| 500 | numInvokesRequestedLock.Unlock() |
| 501 | record.lock.Lock() |
| 502 | record.nGets++ |
| 503 | record.lock.Unlock() |
| 504 | if shouldFail { |
| 505 | <-ctx.Done() // wait for all handlers to finish. |
| 506 | w.WriteHeader(http.StatusGone) |
| 507 | _, _ = w.Write([]byte("END THE TEST!")) |
| 508 | return |
| 509 | } |
| 510 | w.Header().Add(string(headerAWSRequestID), metadata.requestID) |
| 511 | w.Header().Add(string(headerDeadlineMS), metadata.deadline) |
| 512 | w.Header().Add(string(headerInvokedFunctionARN), metadata.functionARN) |
| 513 | w.Header().Add(string(headerClientContext), metadata.clientContext) |
| 514 | w.Header().Add(string(headerCognitoIdentity), metadata.cognito) |
| 515 | w.Header().Add(string(headerTraceID), metadata.xray) |
| 516 | if metadata.tenantID != "" { |
| 517 | w.Header().Add(string(headerTenantID), metadata.tenantID) |
| 518 | } |
| 519 | w.WriteHeader(http.StatusOK) |
| 520 | _, _ = w.Write([]byte(eventPayload)) |
| 521 | case http.MethodPost: |
| 522 | response := bytes.NewBuffer(nil) |
| 523 | _, _ = io.Copy(response, r.Body) |
| 524 | _ = r.Body.Close() |
| 525 | w.WriteHeader(http.StatusAccepted) |
| 526 | record.lock.Lock() |
| 527 | record.nPosts++ |
| 528 | done := record.nPosts >= failAfter |
| 529 | record.responses = append(record.responses, response.Bytes()) |
| 530 | record.contentTypes = append(record.contentTypes, r.Header.Get("Content-Type")) |
| 531 | record.xrayCauses = append(record.xrayCauses, r.Header.Get(headerXRayErrorCause)) |
| 532 | record.lock.Unlock() |
| 533 | if done { |
| 534 | // all handlers are done, cancel the context to let the GET handler exit. |
| 535 | cancel() |
| 536 | } |
| 537 | default: |
| 538 | w.WriteHeader(http.StatusBadRequest) |
| 539 | } |
| 540 | })) |
| 541 |
no test coverage detected
searching dependent graphs…