Skip to content

Commit 20631b0

Browse files
authored
Merge pull request #12 from lambda-feedback/feature/default_command
Removed path handling of command to allow eval to be default
2 parents 94f3e6b + 9178adb commit 20631b0

2 files changed

Lines changed: 35 additions & 10 deletions

File tree

runtime/handler.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,11 +395,6 @@ func (s *RuntimeHandler) getCommand(req Request) (string, bool) {
395395
return commandStr, true
396396
}
397397

398-
pathElements := strings.Split(strings.TrimPrefix(req.Path, "/"), "/")
399-
if len(pathElements) == 1 && pathElements[0] != "" {
400-
return pathElements[0], true
401-
}
402-
403398
// if no command could be extracted from the request,
404399
// fall back to the `eval` command
405400
return "eval", true

runtime/handler_test.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,37 @@ func TestRuntimeHandler_Handle_InvalidCommand(t *testing.T) {
151151
req := createRequest(http.MethodPost, "/!invalid", []byte(`{}`), http.Header{})
152152
resp := handler.Handle(context.Background(), req)
153153

154-
require.Equal(t, http.StatusBadRequest, resp.StatusCode)
154+
var respBody map[string]any
155+
err = json.Unmarshal(resp.Body, &respBody)
156+
require.NoError(t, err)
157+
158+
responseError := respBody["error"].(map[string]interface{})
159+
160+
require.Equal(t, "request validation error", responseError["message"])
161+
}
162+
163+
func TestRuntimeHandler_Handle_Empty_Command(t *testing.T) {
164+
mockResponse := runtime.EvaluationResponse{
165+
"command": "eval",
166+
"result": map[string]interface{}{
167+
"is_correct": true,
168+
"feedback": "Well done! Your answer is correct.",
169+
},
170+
}
171+
172+
handler := setupHandlerWithStaticMock(t, mockResponse)
173+
174+
body := createRequestBody(t, map[string]any{
175+
"response": 1,
176+
"answer": 1,
177+
})
178+
179+
req := createRequest(http.MethodPost, "", body, http.Header{})
180+
181+
resp := handler.Handle(context.Background(), req)
182+
respBody := parseResponseBody(t, resp)
183+
184+
require.Equal(t, mockResponse["result"], respBody["result"])
155185
}
156186

157187
func TestRuntimeHandler_Handle_InvalidMethod(t *testing.T) {
@@ -416,8 +446,8 @@ func TestRunTimeHandler_Healthcheck(t *testing.T) {
416446
handler := setupHandlerWithStaticMock(t, mockResponse)
417447
body := createRequestBody(t, map[string]any{})
418448

419-
req := createRequest(http.MethodPost, "/healthcheck", body, http.Header{
420-
"command": []string{"healthcheck"},
449+
req := createRequest(http.MethodPost, "/", body, http.Header{
450+
"Command": []string{"healthcheck"},
421451
})
422452

423453
resp := handler.Handle(context.Background(), req)
@@ -443,7 +473,7 @@ func TestRunTimeHandler_Valid_Preview(t *testing.T) {
443473
})
444474

445475
req := createRequest(http.MethodPost, "/preview", body, http.Header{
446-
"command": []string{"preview"},
476+
"Command": []string{"preview"},
447477
})
448478

449479
resp := handler.Handle(context.Background(), req)
@@ -501,7 +531,7 @@ func TestRunTimeHandler_Invalid_Preview_Incorrect_Args(t *testing.T) {
501531
})
502532

503533
req := createRequest(http.MethodPost, "/preview", body, http.Header{
504-
"command": []string{"preview"},
534+
"Command": []string{"preview"},
505535
})
506536

507537
resp := handler.Handle(context.Background(), req)

0 commit comments

Comments
 (0)