-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcurl_test.go
More file actions
167 lines (145 loc) · 4.42 KB
/
curl_test.go
File metadata and controls
167 lines (145 loc) · 4.42 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) Codesphere Inc.
// SPDX-License-Identifier: Apache-2.0
package cmd_test
import (
"fmt"
"time"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/stretchr/testify/mock"
"github.com/codesphere-cloud/cs-go/api"
"github.com/codesphere-cloud/cs-go/cli/cmd"
)
var _ = Describe("Curl", func() {
var (
mockEnv *cmd.MockEnv
mockClient *cmd.MockClient
mockExecutor *cmd.MockCommandExecutor
c *cmd.CurlCmd
wsId int
teamId int
token string
devDomain string
workspace api.Workspace
)
JustBeforeEach(func() {
mockClient = cmd.NewMockClient(GinkgoT())
mockEnv = cmd.NewMockEnv(GinkgoT())
mockExecutor = cmd.NewMockCommandExecutor(GinkgoT())
wsId = 42
teamId = 21
token = "test-api-token"
devDomain = "42-3000.dev.5.codesphere.com"
workspace = api.Workspace{
Id: wsId,
TeamId: teamId,
Name: "test-workspace",
DevDomain: &devDomain,
}
c = &cmd.CurlCmd{
Opts: cmd.CurlOptions{
GlobalOptions: &cmd.GlobalOptions{
Env: mockEnv,
WorkspaceId: wsId,
},
Timeout: 30 * time.Second,
Executor: mockExecutor,
},
}
})
Context("CurlWorkspace", func() {
It("should construct the correct URL with default port", func() {
mockClient.EXPECT().GetWorkspace(wsId).Return(workspace, nil)
mockExecutor.EXPECT().Execute(
mock.Anything,
"curl",
mock.MatchedBy(func(args []string) bool {
// Verify the args contain the expected header, flag, -L and URL
hasFollowRedirects := false
hasHeader := false
hasFlag := false
hasURL := false
for i, arg := range args {
if arg == "-L" {
hasFollowRedirects = true
}
if arg == "-H" && i+1 < len(args) && args[i+1] == fmt.Sprintf("X-CS-Authorization: Bearer %s", token) {
hasHeader = true
}
if arg == "-I" {
hasFlag = true
}
if arg == "https://42-3000.dev.5.codesphere.com/api/health" {
hasURL = true
}
}
return hasFollowRedirects && hasHeader && hasFlag && hasURL
}),
mock.Anything,
mock.Anything,
).Return(nil)
err := c.CurlWorkspace(mockClient, wsId, token, "/api/health", []string{"-I"})
Expect(err).ToNot(HaveOccurred())
})
It("should construct the correct URL with custom path", func() {
mockClient.EXPECT().GetWorkspace(wsId).Return(workspace, nil)
mockExecutor.EXPECT().Execute(
mock.Anything,
"curl",
mock.MatchedBy(func(args []string) bool {
// Verify the URL contains the custom path and -L flag
hasFollowRedirects := false
hasHeader := false
hasURL := false
for i, arg := range args {
if arg == "-L" {
hasFollowRedirects = true
}
if arg == "-H" && i+1 < len(args) && args[i+1] == fmt.Sprintf("X-CS-Authorization: Bearer %s", token) {
hasHeader = true
}
if arg == "https://42-3000.dev.5.codesphere.com/custom/path" {
hasURL = true
}
}
return hasFollowRedirects && hasHeader && hasURL
}),
mock.Anything,
mock.Anything,
).Return(nil)
err := c.CurlWorkspace(mockClient, wsId, token, "/custom/path", []string{})
Expect(err).ToNot(HaveOccurred())
})
It("should return error if workspace has no dev domain", func() {
workspace := api.Workspace{
Id: wsId,
TeamId: teamId,
Name: "test-workspace",
DevDomain: nil,
}
mockClient.EXPECT().GetWorkspace(wsId).Return(workspace, nil)
err := c.CurlWorkspace(mockClient, wsId, token, "/", []string{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("does not have a dev domain configured"))
})
It("should return error if GetWorkspace fails", func() {
mockClient.EXPECT().GetWorkspace(wsId).Return(api.Workspace{}, fmt.Errorf("api error"))
err := c.CurlWorkspace(mockClient, wsId, token, "/", []string{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("failed to get workspace"))
})
It("should return error if command execution fails", func() {
mockClient.EXPECT().GetWorkspace(wsId).Return(workspace, nil)
mockExecutor.EXPECT().Execute(
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
mock.Anything,
).Return(fmt.Errorf("command failed"))
err := c.CurlWorkspace(mockClient, wsId, token, "/", []string{})
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("command failed"))
})
})
})