Repository: apple/swift-nio
File: Tests/NIOHTTP1Tests/ContentLengthTests.swift (Swift)

Code before:
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2017-2021 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import XCTest
import NIOCore
import NIOEmbedded
import NIOHTTP1

class ContentLengthTests: XCTestCase {

    /// Client receives a response longer than the content-length header
    func testResponseContentTooLong() throws {
        let channel = EmbeddedChannel()
        try channel.pipeline.syncOperations.addHTTPClientHandlers()
        defer {
            _ = try? channel.finish()
        }
        // Receive a response with a content-length header of 2 but a body of more than 2 bytes
        let badResponse = "HTTP/1.1 200 OK\r\nServer: foo\r\nContent-Length: 2\r\n\r\ntoo many bytes"

        XCTAssertThrowsError(try channel.sendRequestAndReceiveResponse(response: badResponse)) { error in
            XCTAssertEqual(error as? HTTPParserError, .invalidConstant)
        }

        channel.embeddedEventLoop.run()
    }

    /// Client receives a response shorter than the content-length header
    func testResponseContentTooShort() throws {
        let channel = EmbeddedChannel()
        try channel.pipeline.syncOperations.addHTTPClientHandlers()
        defer {
            _ = try? channel.finish()
        }

Review comment (suggestion):
```suggestion
final class ContentLengthTests: XCTestCase {
```

Code after:
//===----------------------------------------------------------------------===//
//
// This source file is part of the SwiftNIO open source project
//
// Copyright (c) 2023 Apple Inc. and the SwiftNIO project authors
// Licensed under Apache License v2.0
//
// See LICENSE.txt for license information
// See CONTRIBUTORS.txt for the list of SwiftNIO project authors
//
// SPDX-License-Identifier: Apache-2.0
//
//===----------------------------------------------------------------------===//

import XCTest
import NIOCore
import NIOEmbedded
import NIOHTTP1

final class ContentLengthTests: XCTestCase {

    /// Client receives a response longer than the content-length header
    func testResponseContentTooLong() throws {
        let channel = EmbeddedChannel()
        try channel.pipeline.syncOperations.addHTTPClientHandlers()
        defer {
            _ = try? channel.finish()
        }
        // Receive a response with a content-length header of 2 but a body of more than 2 bytes
        let badResponse = "HTTP/1.1 200 OK\r\nServer: foo\r\nContent-Length: 2\r\n\r\ntoo many bytes"

        XCTAssertThrowsError(try channel.sendRequestAndReceiveResponse(response: badResponse)) { error in
            XCTAssertEqual(error as? HTTPParserError, .invalidConstant)
        }

        channel.embeddedEventLoop.run()
    }

    /// Client receives a response shorter than the content-length header
    func testResponseContentTooShort() throws {
        let channel = EmbeddedChannel()
        try channel.pipeline.syncOperations.addHTTPClientHandlers()
        defer {
            _ = try? channel.finish()
        }