@@ -17,7 +17,6 @@ limitations under the License.
17
17
package http
18
18
19
19
import (
20
- "bytes"
21
20
"context"
22
21
"crypto/sha256"
23
22
"encoding/base64"
@@ -26,6 +25,7 @@ import (
26
25
"fmt"
27
26
"net/http"
28
27
"net/http/httptest"
28
+ "regexp"
29
29
"testing"
30
30
"time"
31
31
@@ -105,22 +105,15 @@ func TestValidate(t *testing.T) {
105
105
for _ , tc := range testCases {
106
106
t .Run (tc .name , func (t * testing.T ) {
107
107
resolver := Resolver {}
108
- params := []pipelinev1. Param {}
108
+ params := map [ string ] string {}
109
109
if tc .url != "nourl" {
110
- params = append (params , pipelinev1.Param {
111
- Name : "url" ,
112
- Value : * pipelinev1 .NewStructuredValues (tc .url ),
113
- })
110
+ params [httpresolution .UrlParam ] = tc .url
114
111
} else {
115
112
// inject a fake param so that it can validate that the url is actually missing.
116
- params = append (params , pipelinev1.Param {
117
- Name : "foo" ,
118
- Value : * pipelinev1 .NewStructuredValues ("bar" ),
119
- })
113
+ params ["foo" ] = "bar"
120
114
}
121
-
122
115
req := v1beta1.ResolutionRequestSpec {
123
- Params : params ,
116
+ Params : toParams ( params ) ,
124
117
}
125
118
err := resolver .Validate (contextWithConfig (defaultHttpTimeoutValue ), & req )
126
119
if tc .expectedErr != nil {
@@ -133,115 +126,79 @@ func TestValidate(t *testing.T) {
133
126
}
134
127
135
128
func TestResolve (t * testing.T ) {
136
- testCases := []struct {
129
+ tests := []struct {
137
130
name string
138
- expectedError error
131
+ expectedErr string
139
132
input string
140
- paramsSet bool
141
- useCache bool
133
+ paramSet bool
142
134
expectedStatus int
143
135
}{
144
136
{
145
- name : "valid params set with cache" ,
146
- expectedError : nil ,
147
- input : "https://example.com/script.yaml" ,
148
- paramsSet : true ,
149
- useCache : true ,
150
- expectedStatus : http .StatusOK ,
151
- },
152
- {
153
- name : "valid params set without cache" ,
154
- expectedError : nil ,
155
- input : "https://example.com/script.yaml" ,
156
- paramsSet : true ,
157
- useCache : false ,
158
- expectedStatus : http .StatusOK ,
159
- },
160
- {
161
- name : "missing required params" ,
162
- expectedError : fmt .Errorf ("missing required param: url" ),
163
- input : "" ,
164
- paramsSet : false ,
165
- useCache : false ,
166
- expectedStatus : http .StatusBadRequest ,
167
- },
168
- {
169
- name : "not found error" ,
170
- expectedError : fmt .Errorf ("error accessing resource from \" https://example.com/not-found.yaml\" : 404 Not Found" ),
171
- input : "https://example.com/not-found.yaml" ,
172
- paramsSet : true ,
173
- useCache : false ,
137
+ name : "good/params set" ,
138
+ input : "task" ,
139
+ paramSet : true ,
140
+ }, {
141
+ name : "bad/params not set" ,
142
+ input : "task" ,
143
+ expectedErr : `missing required http resolver params: url` ,
144
+ }, {
145
+ name : "bad/not found" ,
146
+ input : "task" ,
147
+ paramSet : true ,
174
148
expectedStatus : http .StatusNotFound ,
149
+ expectedErr : `requested URL 'http://([^']*)' is not found` ,
175
150
},
176
151
}
177
-
178
- for _ , tc := range testCases {
152
+ for _ , tc := range tests {
179
153
t .Run (tc .name , func (t * testing.T ) {
180
- server := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
181
- w .WriteHeader (tc .expectedStatus )
182
- if tc .expectedStatus == http .StatusOK {
183
- w .Write ([]byte ("test content" ))
154
+ svr := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
155
+ if tc .expectedStatus != 0 {
156
+ w .WriteHeader (tc .expectedStatus )
184
157
}
158
+ fmt .Fprint (w , tc .input )
185
159
}))
186
- defer server .Close ()
187
-
188
- resolver := & Resolver {}
189
160
params := []pipelinev1.Param {}
190
- if tc .paramsSet {
161
+ if tc .paramSet {
191
162
params = append (params , pipelinev1.Param {
192
- Name : "url" ,
193
- Value : * pipelinev1 .NewStructuredValues (tc .input ),
163
+ Name : httpresolution .UrlParam ,
164
+ Value : * pipelinev1 .NewStructuredValues (svr .URL ),
165
+ })
166
+ } else {
167
+ params = append (params , pipelinev1.Param {
168
+ Name : "foo" ,
169
+ Value : * pipelinev1 .NewStructuredValues ("bar" ),
194
170
})
195
- if tc .useCache {
196
- params = append (params , pipelinev1.Param {
197
- Name : "cache" ,
198
- Value : * pipelinev1 .NewStructuredValues ("true" ),
199
- })
200
- }
201
171
}
202
-
172
+ resolver := Resolver {}
203
173
req := v1beta1.ResolutionRequestSpec {
204
174
Params : params ,
205
175
}
206
-
207
- ctx := contextWithConfig (defaultHttpTimeoutValue )
208
- resolved , err := resolver .Resolve (ctx , & req )
209
-
210
- if tc .expectedError != nil {
211
- if err == nil {
212
- t .Errorf ("expected error %v but got nil" , tc .expectedError )
213
- } else if err .Error () != tc .expectedError .Error () {
214
- t .Errorf ("expected error %v but got %v" , tc .expectedError , err )
176
+ output , err := resolver .Resolve (contextWithConfig (defaultHttpTimeoutValue ), & req )
177
+ if tc .expectedErr != "" {
178
+ re := regexp .MustCompile (tc .expectedErr )
179
+ if ! re .MatchString (err .Error ()) {
180
+ t .Fatalf ("expected error '%v' but got '%v'" , tc .expectedErr , err )
215
181
}
216
182
return
183
+ } else if err != nil {
184
+ t .Fatalf ("unexpected error validating params: %v" , err )
217
185
}
218
-
219
- if err != nil {
220
- t .Errorf ("unexpected error: %v" , err )
221
- return
186
+ if o := cmp .Diff (tc .input , string (output .Data ())); o != "" {
187
+ t .Fatalf ("expected output '%v' but got '%v'" , tc .input , string (output .Data ()))
188
+ }
189
+ if o := cmp .Diff (svr .URL , output .RefSource ().URI ); o != "" {
190
+ t .Fatalf ("expected url '%v' but got '%v'" , svr .URL , output .RefSource ().URI )
222
191
}
223
192
224
- if resolved == nil {
225
- t .Error ("expected resolved resource but got nil" )
226
- return
193
+ eSum := sha256 .New ()
194
+ eSum .Write ([]byte (tc .input ))
195
+ eSha256 := hex .EncodeToString (eSum .Sum (nil ))
196
+ if o := cmp .Diff (eSha256 , output .RefSource ().Digest ["sha256" ]); o != "" {
197
+ t .Fatalf ("expected sha256 '%v' but got '%v'" , eSha256 , output .RefSource ().Digest ["sha256" ])
227
198
}
228
199
229
- // Test cache functionality
230
- if tc .useCache {
231
- // Try resolving again with the same parameters
232
- cached , err := resolver .Resolve (ctx , & req )
233
- if err != nil {
234
- t .Errorf ("unexpected error when resolving cached resource: %v" , err )
235
- return
236
- }
237
- if cached == nil {
238
- t .Error ("expected cached resource but got nil" )
239
- return
240
- }
241
- // Verify that the cached resource matches the original
242
- if ! bytes .Equal (resolved .Data (), cached .Data ()) {
243
- t .Error ("cached resource does not match original resource" )
244
- }
200
+ if output .Annotations () != nil {
201
+ t .Fatalf ("output annotations should be nil" )
245
202
}
246
203
})
247
204
}
0 commit comments