aboutsummaryrefslogtreecommitdiffhomepage
path: root/worker/swift/handlers_test.go
blob: f9c2e0bd6ffcbb3c4cccad0becc5045bcc699931 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main

import (
	"errors"
	"net/http"
	"net/http/httptest"
	"strings"
	"testing"

	"github.com/labstack/echo/v4"
)

func TestNewBadRequestError(t *testing.T) {
	httpErr := newBadRequestError(errors.New("test error"))
	if httpErr.Code != http.StatusBadRequest {
		t.Errorf("status code = %d, want %d", httpErr.Code, http.StatusBadRequest)
	}
	msg, ok := httpErr.Message.(string)
	if !ok {
		t.Fatalf("expected string message, got %T", httpErr.Message)
	}
	if !strings.Contains(msg, "test error") {
		t.Errorf("message = %q, want it to contain %q", msg, "test error")
	}
}

func TestHandleExec_InvalidJSON(t *testing.T) {
	e := echo.New()
	req := httptest.NewRequest(http.MethodPost, "/exec", strings.NewReader("not json"))
	req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
	rec := httptest.NewRecorder()
	c := e.NewContext(req, rec)

	err := handleExec(c)
	if err == nil {
		t.Fatal("expected error, got nil")
	}
	httpErr, ok := err.(*echo.HTTPError)
	if !ok {
		t.Fatalf("expected *echo.HTTPError, got %T", err)
	}
	if httpErr.Code != http.StatusBadRequest {
		t.Errorf("status code = %d, want %d", httpErr.Code, http.StatusBadRequest)
	}
}

func TestHandleExec_ZeroMaxDuration(t *testing.T) {
	e := echo.New()
	body := `{"code":"print(1)","code_hash":"abc","stdin":"","max_duration_ms":0}`
	req := httptest.NewRequest(http.MethodPost, "/exec", strings.NewReader(body))
	req.Header.Set(echo.HeaderContentType, echo.MIMEApplicationJSON)
	rec := httptest.NewRecorder()
	c := e.NewContext(req, rec)

	err := handleExec(c)
	if err == nil {
		t.Fatal("expected error, got nil")
	}
	httpErr, ok := err.(*echo.HTTPError)
	if !ok {
		t.Fatalf("expected *echo.HTTPError, got %T", err)
	}
	if httpErr.Code != http.StatusBadRequest {
		t.Errorf("status code = %d, want %d", httpErr.Code, http.StatusBadRequest)
	}
}