(t *testing.T)
| 63 | } |
| 64 | |
| 65 | func TestClientDoneAndError(t *testing.T) { |
| 66 | invokeID := "theid" |
| 67 | |
| 68 | var capturedErrors [][]byte |
| 69 | var capturedResponses [][]byte |
| 70 | |
| 71 | acceptsResponses := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 72 | if r.Method != http.MethodPost { |
| 73 | t.Logf("unexpected method: %s", r.Method) |
| 74 | w.WriteHeader(http.StatusNotImplemented) |
| 75 | return |
| 76 | } |
| 77 | if r.URL.Path != fmt.Sprintf("/2018-06-01/runtime/invocation/%s/error", invokeID) && r.URL.Path != fmt.Sprintf("/2018-06-01/runtime/invocation/%s/response", invokeID) { |
| 78 | t.Logf("unexpected url path: %s", r.URL.Path) |
| 79 | w.WriteHeader(http.StatusNotFound) |
| 80 | return |
| 81 | } |
| 82 | body, _ := ioutil.ReadAll(r.Body) |
| 83 | if strings.HasSuffix(r.URL.Path, "/error") { |
| 84 | capturedErrors = append(capturedErrors, body) |
| 85 | } else if strings.HasSuffix(r.URL.Path, "/response") { |
| 86 | capturedResponses = append(capturedErrors, body) |
| 87 | } |
| 88 | w.WriteHeader(http.StatusAccepted) |
| 89 | })) |
| 90 | defer acceptsResponses.Close() |
| 91 | |
| 92 | client := newRuntimeAPIClient(serverAddress(acceptsResponses)) |
| 93 | inputPayloads := [][]byte{nil, {}, []byte("hello")} |
| 94 | expectedPayloadsRecived := [][]byte{{}, {}, []byte("hello")} // nil payload expected to be read as empty bytes by the server |
| 95 | for i, payload := range inputPayloads { |
| 96 | invoke := &invoke{ |
| 97 | id: invokeID, |
| 98 | client: client, |
| 99 | payload: bytes.NewBuffer(nil), |
| 100 | } |
| 101 | t.Run(fmt.Sprintf("happy Done with payload[%d]", i), func(t *testing.T) { |
| 102 | err := invoke.success(bytes.NewReader(payload), contentTypeJSON) |
| 103 | assert.NoError(t, err) |
| 104 | }) |
| 105 | t.Run(fmt.Sprintf("happy Error with payload[%d]", i), func(t *testing.T) { |
| 106 | err := invoke.failure(bytes.NewReader(payload), contentTypeJSON, nil) |
| 107 | assert.NoError(t, err) |
| 108 | }) |
| 109 | } |
| 110 | assert.Equal(t, expectedPayloadsRecived, capturedErrors) |
| 111 | assert.Equal(t, expectedPayloadsRecived, capturedResponses) |
| 112 | } |
| 113 | |
| 114 | func TestInvalidRequestsForMalformedEndpoint(t *testing.T) { |
| 115 | _, err := newRuntimeAPIClient("🚨").next(context.Background()) |
nothing calls this directly
no test coverage detected
searching dependent graphs…