|
| 1 | +import XCTest |
| 2 | + |
| 3 | +@testable import Vapor |
| 4 | + |
| 5 | +import VaporSecurityHeaders |
| 6 | + |
| 7 | +class RedirectionTest: XCTestCase { |
| 8 | + |
| 9 | + // MARK: - Properties |
| 10 | + |
| 11 | + private var application: Application! |
| 12 | + private var eventLoopGroup: EventLoopGroup! |
| 13 | + private var request: Request! |
| 14 | + |
| 15 | + override func setUp() { |
| 16 | + eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1) |
| 17 | + application = Application(.testing, .shared(eventLoopGroup)) |
| 18 | + request = Request(application: application, method: .GET, on: eventLoopGroup.next()) |
| 19 | + } |
| 20 | + |
| 21 | + override func tearDownWithError() throws { |
| 22 | + application.shutdown() |
| 23 | + try eventLoopGroup.syncShutdownGracefully() |
| 24 | + } |
| 25 | + |
| 26 | + func testWithRedirectionMiddleware() throws { |
| 27 | + let expectedRedirectStatus: HTTPStatus = HTTPResponseStatus(statusCode: 301, reasonPhrase: "Moved permanently") |
| 28 | + request.headers.add(name: .host, value: "localhost:8080") |
| 29 | + let responseRedirected = try makeTestResponse(for: request, withRedirection: true) |
| 30 | + XCTAssertEqual(expectedRedirectStatus, responseRedirected.status) |
| 31 | + } |
| 32 | + func testWithoutRedirectionMiddleware() throws { |
| 33 | + let expectedNoRedirectStatus: HTTPStatus = HTTPResponseStatus(statusCode: 200, reasonPhrase: "Ok") |
| 34 | + request.headers.add(name: .host, value: "localhost:8080") |
| 35 | + let response = try makeTestResponse(for: request, withRedirection: false) |
| 36 | + XCTAssertEqual(expectedNoRedirectStatus, response.status) |
| 37 | + } |
| 38 | + |
| 39 | + func testOnDevelopmentEnvironment() throws { |
| 40 | + let expectedStatus: HTTPStatus = HTTPResponseStatus(statusCode: 200, reasonPhrase: "Ok") |
| 41 | + request.headers.add(name: .host, value: "localhost:8080") |
| 42 | + let response = try makeTestResponse(for: request, withRedirection: true, environment: .development) |
| 43 | + XCTAssertEqual(expectedStatus, response.status) |
| 44 | + } |
| 45 | + |
| 46 | + func testWithoutHost() throws { |
| 47 | + let expectedOutcome: String = "Abort.400: Bad Request" |
| 48 | + do { |
| 49 | + _ = try makeTestResponse(for: request, withRedirection: true) |
| 50 | + } catch (let error) { |
| 51 | + XCTAssertEqual(expectedOutcome, error.localizedDescription) |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + func testWithProtoSet() throws { |
| 56 | + let expectedStatus: HTTPStatus = HTTPResponseStatus(statusCode: 200, reasonPhrase: "Ok") |
| 57 | + request.headers.add(name: .xForwardedProto, value: "https") |
| 58 | + let response = try makeTestResponse(for: request, withRedirection: true) |
| 59 | + XCTAssertEqual(expectedStatus, response.status) |
| 60 | + } |
| 61 | + |
| 62 | + private func makeTestResponse(for request: Request, withRedirection: Bool, environment: Environment? = nil) throws -> Response { |
| 63 | + application.middleware = Middlewares() |
| 64 | + if let environment = environment { |
| 65 | + application.environment = environment |
| 66 | + } |
| 67 | + if withRedirection == true { |
| 68 | + application.middleware.use(HTTPSRedirectMiddleware()) |
| 69 | + } |
| 70 | + try routes(application) |
| 71 | + return try application.responder.respond(to: request).wait() |
| 72 | + } |
| 73 | + |
| 74 | + func routes(_ app: Application) throws { |
| 75 | + try app.register(collection: RouteController()) |
| 76 | + } |
| 77 | + |
| 78 | + struct RouteController: RouteCollection { |
| 79 | + func boot(routes: RoutesBuilder) throws { |
| 80 | + routes.get(use: testing) |
| 81 | + } |
| 82 | + func testing(req: Request) throws -> String { |
| 83 | + return "Test" |
| 84 | + } |
| 85 | + } |
| 86 | +} |
0 commit comments