-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ea67722
commit 5bed557
Showing
17 changed files
with
169 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import ArgumentParser | ||
import Hummingbird | ||
|
||
@main | ||
struct MultipartFormApp: AsyncParsableCommand, AppArguments { | ||
@Option(name: .shortAndLong) | ||
var hostname: String = "127.0.0.1" | ||
|
||
@Option(name: .shortAndLong) | ||
var port: Int = 8080 | ||
|
||
func run() async throws { | ||
let app = try await buildApplication(self) | ||
try await app.runService() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import Hummingbird | ||
import MultipartKit | ||
import Mustache | ||
|
||
protocol AppArguments { | ||
var hostname: String { get } | ||
var port: Int { get } | ||
} | ||
|
||
func buildApplication(_ args: AppArguments) async throws -> some ApplicationProtocol { | ||
let library = try await MustacheLibrary(directory: "templates") | ||
assert(library.getTemplate(named: "page") != nil, "Set your working directory to the root folder of this example to get it to work") | ||
|
||
let router = Router(context: MultipartRequestContext.self) | ||
router.middlewares.add(FileMiddleware()) | ||
WebController(mustacheLibrary: library).addRoutes(to: router) | ||
return Application(router: router, configuration: .init(address: .hostname(args.hostname, port: args.port))) | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import Hummingbird | ||
import Logging | ||
import NIOCore | ||
|
||
struct MultipartRequestContext: RequestContext { | ||
var requestDecoder: MultipartRequestDecoder { .init() } | ||
var coreContext: CoreRequestContext | ||
|
||
init(channel: Channel, logger: Logger) { | ||
self.coreContext = .init(allocator: channel.allocator, logger: logger) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,37 @@ | ||
import App | ||
@testable import App | ||
import Hummingbird | ||
import HummingbirdXCT | ||
import HummingbirdTesting | ||
import XCTest | ||
|
||
final class AppTests: XCTestCase { | ||
func testApp() throws { | ||
let app = HBApplication(testing: .live) | ||
try app.configure() | ||
|
||
try app.XCTStart() | ||
defer { app.XCTStop() } | ||
struct TestArguments: AppArguments { | ||
let hostname = "127.0.0.1" | ||
let port = 8080 | ||
} | ||
|
||
let multipartForm = """ | ||
------HBTestFormBoundaryXD6BXJI\r | ||
Content-Disposition: form-data; name="name"\r | ||
\r | ||
adam\r | ||
------HBTestFormBoundaryXD6BXJI\r | ||
Content-Disposition: form-data; name="age"\r | ||
\r | ||
50\r | ||
------HBTestFormBoundaryXD6BXJI--\r | ||
""" | ||
let contentType = "multipart/form-data; boundary=----HBTestFormBoundaryXD6BXJI" | ||
try app.XCTExecute( | ||
uri: "/", | ||
method: .POST, | ||
headers: ["content-type": contentType], | ||
body: ByteBufferAllocator().buffer(string: multipartForm) | ||
) { response in | ||
XCTAssertEqual(response.status, .ok) | ||
func testApp() async throws { | ||
let app = try await buildApplication(TestArguments()) | ||
try await app.test(.router) { client in | ||
let multipartForm = """ | ||
------HBTestFormBoundaryXD6BXJI\r | ||
Content-Disposition: form-data; name="name"\r | ||
\r | ||
adam\r | ||
------HBTestFormBoundaryXD6BXJI\r | ||
Content-Disposition: form-data; name="age"\r | ||
\r | ||
50\r | ||
------HBTestFormBoundaryXD6BXJI--\r | ||
""" | ||
let contentType = "multipart/form-data; boundary=----HBTestFormBoundaryXD6BXJI" | ||
try await client.execute( | ||
uri: "/", | ||
method: .post, | ||
headers: [.contentType: contentType], | ||
body: ByteBufferAllocator().buffer(string: multipartForm) | ||
) { response in | ||
XCTAssertEqual(response.status, .ok) | ||
} | ||
} | ||
} | ||
} |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,9 @@ | ||
{{>head}} | ||
<body> | ||
<div> | ||
<h1> | ||
You entered | ||
</h1> | ||
<ul> | ||
<li>Name: {{name}}</li> | ||
<li>Age: {{age}}</li> | ||
</ul> | ||
</div> | ||
</body> | ||
</html> | ||
{{<page}} | ||
{{$body}} | ||
<h1 class="text-xl">You entered</h1> | ||
<ul> | ||
<li>Name: {{name}}</li> | ||
<li>Age: {{age}}</li> | ||
</ul> | ||
{{/body}} | ||
{{/page}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,12 @@ | ||
{{>head}} | ||
<body> | ||
<div> | ||
<h1>Please enter your details</h1> | ||
<form action="/" method="post" enctype="multipart/form-data"> | ||
<label for="name">Name</label><br/> | ||
<input type="text" id="name" name="name"/><br/> | ||
<label for="age">Age</label><br/> | ||
<input type="text" id="age" name="age"/><br/> | ||
<input type="submit" value="Submit"/> | ||
</form> | ||
</div> | ||
</body> | ||
</html> | ||
{{<page}} | ||
{{$body}} | ||
<h1 class="text-xl">Please enter your details</h1> | ||
<form action="/" method="post" enctype="multipart/form-data"> | ||
<label for="name">Name</label><br/> | ||
<input type="text" id="name" name="name" class="border"/><br/> | ||
<label for="age">Age</label><br/> | ||
<input type="text" id="age" name="age" class="border"/><br/> | ||
<input type="submit" value="Submit" class="p-1 my-2 border shadow-lg" /> | ||
</form> | ||
{{/body}} | ||
{{/page}} |
Oops, something went wrong.