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
|
/*
This source file is part of the Swift.org open source project
Copyright (c) 2021 Apple Inc. and the Swift project authors
Licensed under Apache License v2.0 with Runtime Library Exception
See https://swift.org/LICENSE.txt for license information
See https://swift.org/CONTRIBUTORS.txt for Swift project authors
*/
#if canImport(NIOHTTP1)
import Foundation
import XCTest
@testable import SwiftDocC
@testable import SwiftDocCUtilities
import SwiftDocCTestUtilities
import NIO
import NIOHTTP1
class PreviewHTTPHandlerTests: XCTestCase {
let fileIO = NonBlockingFileIO(threadPool: NIOThreadPool(numberOfThreads: 2))
/// Tests the three different responses we offer: static file, default, and error.
func testPreviewHandler() throws {
let tempFolderURL = try createTempFolder(content: [
TextFile(name: "index.html", utf8Content: "index"),
Folder(name: "css", content: [
TextFile(name: "test.css", utf8Content: "css"),
])
])
let channel = EmbeddedChannel()
let channelHandler = PreviewHTTPHandler(fileIO: fileIO, rootURL: tempFolderURL)
let response = Response()
XCTAssertNoThrow(try channel.pipeline.addHandler(HTTPResponseEncoder()).wait())
XCTAssertNoThrow(try channel.pipeline.addHandler(response).wait())
XCTAssertNoThrow(try channel.pipeline.addHandler(channelHandler).wait())
XCTAssertNoThrow(try channel.pipeline.addHandler(HTTPServerPipelineHandler()).wait())
XCTAssertNoThrow(try channel.connect(to: SocketAddress(ipAddress: "127.0.0.1", port: 1)).wait())
// Request a page
do {
let request = makeRequestHead(uri: "/tutorials")
XCTAssertNoThrow(try channel.writeInbound(HTTPServerRequestPart.head(request)))
XCTAssertNoThrow(try channel.writeInbound(HTTPServerRequestPart.end(nil)))
XCTAssertEqual(response.head?.status, .ok)
XCTAssertEqual(response.body, "index")
}
// Request an asset
do {
let request = makeRequestHead(uri: "/css/test.css")
XCTAssertNoThrow(try channel.writeInbound(HTTPServerRequestPart.head(request)))
XCTAssertNoThrow(try channel.writeInbound(HTTPServerRequestPart.end(nil)))
XCTAssertEqual(response.head?.status, .ok)
XCTAssertEqual(response.body, "css")
}
// Not found error
do {
let request = makeRequestHead(uri: "/css/notfound.css")
XCTAssertNoThrow(try channel.writeInbound(HTTPServerRequestPart.head(request)))
XCTAssertNoThrow(try channel.writeInbound(HTTPServerRequestPart.end(nil)))
XCTAssertEqual(response.head?.status, .notFound)
XCTAssertEqual(response.body, "")
}
// Passed credentials when none required
do {
let request = makeRequestHead(uri: "/tutorials", headers: [("Authorization", "Basic \("USER:PASS".data(using: .utf8)!.base64EncodedString())")])
XCTAssertNoThrow(try channel.writeInbound(HTTPServerRequestPart.head(request)))
XCTAssertNoThrow(try channel.writeInbound(HTTPServerRequestPart.end(nil)))
XCTAssertEqual(response.head?.status, .ok)
XCTAssertEqual(response.body, "index")
}
}
}
#endif
|