-
Notifications
You must be signed in to change notification settings - Fork 125
Verify header field names #191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
artemredkin
merged 2 commits into
swift-server:master
from
fabianfett:ff-verify-header-field-names
Apr 1, 2020
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
38 changes: 38 additions & 0 deletions
38
Tests/AsyncHTTPClientTests/RequestValidationTests+XCTest.swift
This file contains hidden or 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,38 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2018-2019 Apple Inc. and the AsyncHTTPClient project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// RequestValidationTests+XCTest.swift | ||
// | ||
import XCTest | ||
|
||
/// | ||
/// NOTE: This file was generated by generate_linux_tests.rb | ||
/// | ||
/// Do NOT edit this file directly as it will be regenerated automatically when needed. | ||
/// | ||
|
||
extension RequestValidationTests { | ||
static var allTests: [(String, (RequestValidationTests) -> () throws -> Void)] { | ||
return [ | ||
("testContentLengthHeaderIsRemovedFromGETIfNoBody", testContentLengthHeaderIsRemovedFromGETIfNoBody), | ||
("testContentLengthHeaderIsAddedToPOSTAndPUTWithNoBody", testContentLengthHeaderIsAddedToPOSTAndPUTWithNoBody), | ||
("testContentLengthHeaderIsChangedIfBodyHasDifferentLength", testContentLengthHeaderIsChangedIfBodyHasDifferentLength), | ||
("testChunkedEncodingDoesNotHaveContentLengthHeader", testChunkedEncodingDoesNotHaveContentLengthHeader), | ||
("testTRACERequestMustNotHaveBody", testTRACERequestMustNotHaveBody), | ||
("testGET_HEAD_DELETE_CONNECTRequestCanHaveBody", testGET_HEAD_DELETE_CONNECTRequestCanHaveBody), | ||
("testInvalidHeaderFieldNames", testInvalidHeaderFieldNames), | ||
("testValidHeaderFieldNames", testValidHeaderFieldNames), | ||
] | ||
} | ||
} |
107 changes: 107 additions & 0 deletions
107
Tests/AsyncHTTPClientTests/RequestValidationTests.swift
This file contains hidden or 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,107 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the AsyncHTTPClient open source project | ||
// | ||
// Copyright (c) 2020 Apple Inc. and the AsyncHTTPClient project authors | ||
// Licensed under Apache License v2.0 | ||
// | ||
// See LICENSE.txt for license information | ||
// See CONTRIBUTORS.txt for the list of AsyncHTTPClient project authors | ||
// | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
@testable import AsyncHTTPClient | ||
import NIO | ||
import NIOHTTP1 | ||
import XCTest | ||
|
||
class RequestValidationTests: XCTestCase { | ||
func testContentLengthHeaderIsRemovedFromGETIfNoBody() { | ||
var headers = HTTPHeaders([("Content-Length", "0")]) | ||
XCTAssertNoThrow(try headers.validate(method: .GET, body: .none)) | ||
XCTAssertNil(headers.first(name: "Content-Length")) | ||
} | ||
|
||
func testContentLengthHeaderIsAddedToPOSTAndPUTWithNoBody() { | ||
var putHeaders = HTTPHeaders() | ||
XCTAssertNoThrow(try putHeaders.validate(method: .PUT, body: .none)) | ||
XCTAssertEqual(putHeaders.first(name: "Content-Length"), "0") | ||
|
||
var postHeaders = HTTPHeaders() | ||
XCTAssertNoThrow(try postHeaders.validate(method: .POST, body: .none)) | ||
XCTAssertEqual(postHeaders.first(name: "Content-Length"), "0") | ||
} | ||
|
||
func testContentLengthHeaderIsChangedIfBodyHasDifferentLength() { | ||
var headers = HTTPHeaders([("Content-Length", "0")]) | ||
var buffer = ByteBufferAllocator().buffer(capacity: 200) | ||
buffer.writeBytes([UInt8](repeating: 12, count: 200)) | ||
XCTAssertNoThrow(try headers.validate(method: .PUT, body: .byteBuffer(buffer))) | ||
XCTAssertEqual(headers.first(name: "Content-Length"), "200") | ||
} | ||
|
||
func testChunkedEncodingDoesNotHaveContentLengthHeader() { | ||
var headers = HTTPHeaders([ | ||
("Content-Length", "200"), | ||
("Transfer-Encoding", "chunked"), | ||
]) | ||
var buffer = ByteBufferAllocator().buffer(capacity: 200) | ||
buffer.writeBytes([UInt8](repeating: 12, count: 200)) | ||
XCTAssertNoThrow(try headers.validate(method: .PUT, body: .byteBuffer(buffer))) | ||
|
||
// https://tools.ietf.org/html/rfc7230#section-3.3.2 | ||
// A sender MUST NOT send a Content-Length header field in any message | ||
// that contains a Transfer-Encoding header field. | ||
|
||
XCTAssertNil(headers.first(name: "Content-Length")) | ||
XCTAssertEqual(headers.first(name: "Transfer-Encoding"), "chunked") | ||
} | ||
|
||
func testTRACERequestMustNotHaveBody() { | ||
var headers = HTTPHeaders([ | ||
("Content-Length", "200"), | ||
("Transfer-Encoding", "chunked"), | ||
]) | ||
var buffer = ByteBufferAllocator().buffer(capacity: 200) | ||
buffer.writeBytes([UInt8](repeating: 12, count: 200)) | ||
XCTAssertThrowsError(try headers.validate(method: .TRACE, body: .byteBuffer(buffer))) { | ||
XCTAssertEqual($0 as? HTTPClientError, .traceRequestWithBody) | ||
} | ||
} | ||
|
||
func testGET_HEAD_DELETE_CONNECTRequestCanHaveBody() { | ||
var buffer = ByteBufferAllocator().buffer(capacity: 100) | ||
buffer.writeBytes([UInt8](repeating: 12, count: 100)) | ||
|
||
// GET, HEAD, DELETE and CONNECT requests can have a payload. (though uncommon) | ||
let allowedMethods: [HTTPMethod] = [.GET, .HEAD, .DELETE, .CONNECT] | ||
var headers = HTTPHeaders() | ||
for method in allowedMethods { | ||
XCTAssertNoThrow(try headers.validate(method: method, body: .byteBuffer(buffer))) | ||
} | ||
} | ||
|
||
func testInvalidHeaderFieldNames() { | ||
var headers = HTTPHeaders([ | ||
("Content-Length", "200"), | ||
("User Agent", "Haha"), | ||
]) | ||
|
||
XCTAssertThrowsError(try headers.validate(method: .GET, body: nil)) { error in | ||
XCTAssertEqual(error as? HTTPClientError, HTTPClientError.invalidHeaderFieldNames(["User Agent"])) | ||
} | ||
} | ||
|
||
func testValidHeaderFieldNames() { | ||
var headers = HTTPHeaders([ | ||
("abcdefghijklmnopqrstuvwxyz", "Haha"), | ||
("ABCDEFGHIJKLMNOPQRSTUVWXYZ", "Haha"), | ||
("0123456789", "Haha"), | ||
("!#$%&'*+-.^_`|~", "Haha"), | ||
]) | ||
|
||
XCTAssertNoThrow(try headers.validate(method: .GET, body: nil)) | ||
} | ||
} |
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.