|
2 | 2 | require "../spec_helper" |
3 | 3 | require "colorize" |
4 | 4 | require "../../src/tasks/utils/utils.cr" |
5 | | -require "../../src/tasks/helmenv_setup.cr" |
| 5 | +require "../../src/tasks/setup/helmenv_setup.cr" |
| 6 | +require "kubectl_client" |
6 | 7 | require "file_utils" |
7 | 8 | require "sam" |
8 | 9 | require "json" |
| 10 | +require "http/server" |
9 | 11 |
|
10 | 12 | describe "Utils" do |
11 | 13 | before_all do |
@@ -213,4 +215,104 @@ describe "Utils" do |
213 | 215 | end |
214 | 216 | (response.to_s.size > 0).should be_false |
215 | 217 | end |
| 218 | + |
| 219 | + describe "'download' function", tags: ["utils-download"] do |
| 220 | + port = 50500 |
| 221 | + ip = "0.0.0.0" |
| 222 | + server = HTTP::Server.new do |ctx| |
| 223 | + request = ctx.request |
| 224 | + response = ctx.response |
| 225 | + case request.path |
| 226 | + when "/redirect" |
| 227 | + response.status_code = 302 |
| 228 | + response.headers["Location"] = "http://#{ip}:#{port}/success" |
| 229 | + response.print("redirecting to /success") |
| 230 | + when "/error" |
| 231 | + response.status_code = 404 |
| 232 | + response.print("not found") |
| 233 | + when "/auth" |
| 234 | + auth_header = request.headers["Authorization"] |
| 235 | + if auth_header && auth_header.starts_with?("Bearer ") |
| 236 | + response.status_code = 200 |
| 237 | + response.print("authorized access") |
| 238 | + else |
| 239 | + response.status_code = 401 |
| 240 | + response.print("unauthorized") |
| 241 | + end |
| 242 | + when "/success" |
| 243 | + response.status_code = 200 |
| 244 | + response.print("successful response") |
| 245 | + end |
| 246 | + end |
| 247 | + |
| 248 | + before_all do |
| 249 | + server.bind_tcp(ip, port) |
| 250 | + spawn do |
| 251 | + server.listen |
| 252 | + end |
| 253 | + end |
| 254 | + |
| 255 | + after_all do |
| 256 | + server.close |
| 257 | + end |
| 258 | + |
| 259 | + it "should download file correctly" do |
| 260 | + tempfile = File.tempfile("test_download") |
| 261 | + begin |
| 262 | + download("http://#{ip}:#{port}/success", tempfile.path) |
| 263 | + rescue ex : Exception # Exception should not be raised |
| 264 | + ex.message.should be_nil |
| 265 | + end |
| 266 | + (File.read(tempfile.path) == "successful response").should be_true |
| 267 | + ensure |
| 268 | + tempfile.delete unless tempfile.nil? |
| 269 | + end |
| 270 | + |
| 271 | + it "should download file with redirection correctly" do |
| 272 | + tempfile = File.tempfile("test_download") |
| 273 | + begin |
| 274 | + download("http://#{ip}:#{port}/redirect", tempfile.path) |
| 275 | + rescue ex : Exception # Exception should not be raised |
| 276 | + ex.message.should be_nil |
| 277 | + end |
| 278 | + (File.read(tempfile.path) == "successful response").should be_true |
| 279 | + ensure |
| 280 | + tempfile.delete unless tempfile.nil? |
| 281 | + end |
| 282 | + |
| 283 | + it "should download file with bearer token authentication correctly" do |
| 284 | + tempfile = File.tempfile("test_download") |
| 285 | + headers = HTTP::Headers{"Authorization" => "Bearer valid-token123"} |
| 286 | + begin |
| 287 | + download("http://#{ip}:#{port}/auth", tempfile.path, headers: headers) |
| 288 | + rescue ex : Exception # Exception should not be raised |
| 289 | + ex.message.should be_nil |
| 290 | + end |
| 291 | + (File.read(tempfile.path) == "authorized access").should be_true |
| 292 | + ensure |
| 293 | + tempfile.delete unless tempfile.nil? |
| 294 | + end |
| 295 | + |
| 296 | + it "should raise when 'url' is not found" do |
| 297 | + tempfile = File.tempfile("test_download") |
| 298 | + begin |
| 299 | + download("http://non-existent/path", tempfile.path) |
| 300 | + rescue ex : Exception |
| 301 | + ex.message.should_not be_nil |
| 302 | + end |
| 303 | + ensure |
| 304 | + tempfile.delete unless tempfile.nil? |
| 305 | + end |
| 306 | + |
| 307 | + it "should raise when response is not 2xx" do |
| 308 | + tempfile = File.tempfile("test_download") |
| 309 | + begin |
| 310 | + download("http://#{ip}:#{port}/error", tempfile.path) |
| 311 | + rescue ex : Exception |
| 312 | + (ex.message =~ /Unsuccessful request, status code: \[404\], msg: Not Found/).should_not be_nil |
| 313 | + end |
| 314 | + ensure |
| 315 | + tempfile.delete unless tempfile.nil? |
| 316 | + end |
| 317 | + end |
216 | 318 | end |
0 commit comments