master
go 86 lines 2.08 KB
Raw
1 // SPDX-License-Identifier: GPL-3.0-or-later
2
3 package funcapi
4
5 import (
6 "context"
7 "testing"
8
9 "github.com/stretchr/testify/assert"
10 )
11
12 // mockHandler implements MethodHandler for testing.
13 type mockHandler struct {
14 methods []MethodConfig
15 methodParams []ParamConfig
16 response *FunctionResponse
17 }
18
19 func (m *mockHandler) Methods() []MethodConfig {
20 return m.methods
21 }
22
23 func (m *mockHandler) MethodParams(ctx context.Context, method string) ([]ParamConfig, error) {
24 return m.methodParams, nil
25 }
26
27 func (m *mockHandler) Handle(ctx context.Context, method string, params ResolvedParams) *FunctionResponse {
28 return m.response
29 }
30
31 func (m *mockHandler) Cleanup(ctx context.Context) {}
32
33 func TestMethodHandler_Interface(t *testing.T) {
34 // Verify mockHandler implements MethodHandler
35 var _ MethodHandler = &mockHandler{}
36
37 h := &mockHandler{
38 methods: []MethodConfig{{ID: "test", Name: "Test"}},
39 response: &FunctionResponse{Status: 200},
40 }
41
42 assert.Len(t, h.Methods(), 1)
43 assert.Equal(t, "test", h.Methods()[0].ID)
44
45 params, err := h.MethodParams(context.Background(), "test")
46 assert.NoError(t, err)
47 assert.Nil(t, params)
48
49 resp := h.Handle(context.Background(), "test", nil)
50 assert.Equal(t, 200, resp.Status)
51 }
52
53 func TestErrorResponse(t *testing.T) {
54 resp := ErrorResponse(500, "error: %s", "test")
55
56 assert.Equal(t, 500, resp.Status)
57 assert.Equal(t, "error: test", resp.Message)
58 }
59
60 func TestErrorResponse_NoArgs(t *testing.T) {
61 resp := ErrorResponse(400, "bad request")
62
63 assert.Equal(t, 400, resp.Status)
64 assert.Equal(t, "bad request", resp.Message)
65 }
66
67 func TestNotFoundResponse(t *testing.T) {
68 resp := NotFoundResponse("my-method")
69
70 assert.Equal(t, 404, resp.Status)
71 assert.Contains(t, resp.Message, "my-method")
72 }
73
74 func TestUnavailableResponse(t *testing.T) {
75 resp := UnavailableResponse("data not ready")
76
77 assert.Equal(t, 503, resp.Status)
78 assert.Equal(t, "data not ready", resp.Message)
79 }
80
81 func TestInternalErrorResponse(t *testing.T) {
82 resp := InternalErrorResponse("failed: %v", "connection refused")
83
84 assert.Equal(t, 500, resp.Status)
85 assert.Equal(t, "failed: connection refused", resp.Message)
86 }