|
| 1 | +// |
| 2 | +// RedirectHTTPHandlerTests.swift |
| 3 | +// FlyingFox |
| 4 | +// |
| 5 | +// Created by Simon Whitty on 13/09/2025. |
| 6 | +// Copyright © 2025 Simon Whitty. All rights reserved. |
| 7 | +// |
| 8 | +// Distributed under the permissive MIT license |
| 9 | +// Get the latest version from here: |
| 10 | +// |
| 11 | +// https://github.com/swhitty/FlyingFox |
| 12 | +// |
| 13 | +// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 14 | +// of this software and associated documentation files (the "Software"), to deal |
| 15 | +// in the Software without restriction, including without limitation the rights |
| 16 | +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 17 | +// copies of the Software, and to permit persons to whom the Software is |
| 18 | +// furnished to do so, subject to the following conditions: |
| 19 | +// |
| 20 | +// The above copyright notice and this permission notice shall be included in all |
| 21 | +// copies or substantial portions of the Software. |
| 22 | +// |
| 23 | +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 24 | +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 25 | +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 26 | +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 27 | +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 28 | +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 29 | +// SOFTWARE. |
| 30 | +// |
| 31 | + |
| 32 | +@testable import FlyingFox |
| 33 | +import Foundation |
| 34 | +import Testing |
| 35 | + |
| 36 | +struct RedirectHTTPHandlerTests { |
| 37 | + |
| 38 | + @Test |
| 39 | + func location_is_not_replaced() async throws { |
| 40 | + let handler = RedirectHTTPHandler(location: "http://www.fish.com") |
| 41 | + |
| 42 | + var response = try await handler.handleRequest(.make(path: "/")) |
| 43 | + #expect(response.statusCode == .movedPermanently) |
| 44 | + #expect(response.headers[.location] == "http://www.fish.com") |
| 45 | + |
| 46 | + response = try await handler.handleRequest(.make(path: "/chips", query: [.init(name: "fish", value: "true")])) |
| 47 | + #expect(response.headers[.location] == "http://www.fish.com") |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + func location_statuscode() async throws { |
| 52 | + let handler = RedirectHTTPHandler(location: "http://www.fish.com", statusCode: .temporaryRedirect) |
| 53 | + |
| 54 | + let response = try await handler.handleRequest(.make(path: "/")) |
| 55 | + #expect(response.statusCode == .temporaryRedirect) |
| 56 | + } |
| 57 | + |
| 58 | + @Test |
| 59 | + func base_appends_request() async throws { |
| 60 | + let handler: any HTTPHandler = .redirect(via: "http://fish.com") |
| 61 | + |
| 62 | + var response = try await handler.handleRequest(.make(path: "/chips/shrimp")) |
| 63 | + #expect(response.statusCode == .movedPermanently) |
| 64 | + #expect(response.headers[.location] == "http://fish.com/chips/shrimp") |
| 65 | + |
| 66 | + response = try await handler.handleRequest(.make(path: "/chips/shrimp", query: [.init(name: "fish", value: "true")])) |
| 67 | + #expect(response.headers[.location] == "http://fish.com/chips/shrimp?fish=true") |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + func base_removes_serverPath() async throws { |
| 72 | + let handler: any HTTPHandler = .redirect(via: "http://fish.com", serverPath: "chips/shrimp") |
| 73 | + |
| 74 | + var response = try await handler.handleRequest(.make(path: "/chips/shrimp/1/2", query: [.init(name: "fish", value: "true")])) |
| 75 | + #expect(response.statusCode == .movedPermanently) |
| 76 | + #expect(response.headers[.location] == "http://fish.com/1/2?fish=true") |
| 77 | + |
| 78 | + response = try await handler.handleRequest(.make(path: "/chips/shrimp/1", query: [.init(name: "fish", value: "true")])) |
| 79 | + #expect(response.headers[.location] == "http://fish.com/1?fish=true") |
| 80 | + |
| 81 | + await #expect(throws: URLError.self) { |
| 82 | + try await handler.handleRequest(.make(path: "/foo")) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + func base_statuscode() async throws { |
| 88 | + let handler = RedirectHTTPHandler(base: "http://fish.com", statusCode: .temporaryRedirect, serverPath: "/chips/shrimp") |
| 89 | + |
| 90 | + let response = try await handler.handleRequest(.make(path: "/chips/shrimp")) |
| 91 | + #expect(response.statusCode == .temporaryRedirect) |
| 92 | + } |
| 93 | +} |
0 commit comments