-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrequest_test.go
More file actions
81 lines (70 loc) · 2.22 KB
/
Copy pathrequest_test.go
File metadata and controls
81 lines (70 loc) · 2.22 KB
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package executor
import (
"context"
"strings"
"testing"
"wherobots/cli/internal/config"
"wherobots/cli/internal/spec"
)
func TestBuildRequestInjectsPathQueryBodyAndAuth(t *testing.T) {
t.Parallel()
cfg := config.Config{APIKey: "abc123"}
runtimeSpec := &spec.RuntimeSpec{BaseURL: "https://api.example.com"}
op := &spec.Operation{
Method: "POST",
Path: "/users/{id}",
PathParamOrder: []string{"id"},
QueryParams: []spec.Parameter{
{Name: "expand", Location: "query", Required: true},
},
RequestBody: &spec.RequestBodyInfo{
Required: true,
ContentType: "application/json",
},
}
req, err := BuildRequest(
context.Background(),
cfg,
runtimeSpec,
op,
[]string{"u-1"},
[]QueryPair{{Key: "expand", Value: "true"}},
`{"name":"alice"}`,
)
if err != nil {
t.Fatalf("BuildRequest() error = %v", err)
}
if req.URL.String() != "https://api.example.com/users/u-1?expand=true" {
t.Fatalf("url = %s", req.URL.String())
}
if got := req.Header.Get("x-api-key"); got != "abc123" {
t.Fatalf("x-api-key = %q, want %q", got, "abc123")
}
if got := req.Header.Get("Content-Type"); got != "application/json" {
t.Fatalf("Content-Type = %q, want application/json", got)
}
}
func TestBuildRequestMissingRequiredQueryReturnsError(t *testing.T) {
t.Parallel()
cfg := config.Config{APIKey: "abc123"}
runtimeSpec := &spec.RuntimeSpec{BaseURL: "https://api.example.com"}
op := &spec.Operation{
Method: "GET",
Path: "/users",
QueryParams: []spec.Parameter{{Name: "limit", Location: "query", Required: true}},
}
_, err := BuildRequest(context.Background(), cfg, runtimeSpec, op, nil, nil, "")
if err == nil || !strings.Contains(err.Error(), `missing required query parameter "limit"`) {
t.Fatalf("expected required query error, got %v", err)
}
}
func TestBuildRequestMissingAPIKeyReturnsError(t *testing.T) {
t.Parallel()
cfg := config.Config{}
runtimeSpec := &spec.RuntimeSpec{BaseURL: "https://api.example.com"}
op := &spec.Operation{Method: "GET", Path: "/users"}
_, err := BuildRequest(context.Background(), cfg, runtimeSpec, op, nil, nil, "")
if err == nil || !strings.Contains(err.Error(), "WHEROBOTS_API_KEY") {
t.Fatalf("expected API key error, got %v", err)
}
}