<commit_before>import XCTest
import Foundation
@testable import RequestBuilder

class RequestBuilderTests: XCTestCase {
    
    // MARK: - SUT
    
    let builder = RequestBuilder()
    
    // MARK: - Tests
    
    func testRequestDefaultsToGET() {
        
        // GIVEN
        let url = URL(string: "http://apple.com")
        let expectedRequest = URLRequest(url: url!)
        
        // WHEN
        let request = builder.url(url!).build()
        
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    func testRequestPOST() {
        
        // GIVEN
        let url = URL(string: "http://apple.com")
        var expectedRequest = URLRequest(url: url!)
        expectedRequest.httpMethod = "POST"
        
        // WHEN
        let request = builder.POST()
                        .url(url!)
                        .build()
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    func testRequestPUTAndHeaders() {
        
        // GIVEN
        let url = URL(string: "https://apple.com")
        var expectedRequest = URLRequest(url: url!)
        expectedRequest.httpMethod = "PUT"
        expectedRequest.setValue("Some token", forHTTPHeaderField: "Authorization")
        
        // WHEN
        let request = builder.PUT()
                        .url(url!)
                        .setValue("Some token", for: "Authorization")
                        .build()
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    func testRequestGETWithHeadersAndQueries() {
        
        // GIVEN
        let url = URL(string: "http://api.somehost.com/user_timeline.json?screen_name=cocoapatterns&include_rts=true")
        var expectedRequest = URLRequest(url: url!)
        expectedRequest.setValue("Some token", forHTTPHeaderField: "Authorization")
        
        // WHEN
        let request = builder.GET()
                        .url(URL(string: "http://api.somehost.com/user_timeline.json")!)
                        .setValue("Some token", for: "Authorization")
                        .query(name: "screen_name", value: "cocoapatterns")
                        .query(name: "include_rts", value: "true")
                        .build()
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    func testRequestGETWithEmptyQuery() {
        
        // GIVEN
        let url = URL(string: "http://api.somehost.com/test.json?someQuery=")
        let expectedRequest = URLRequest(url: url!)
    
        // WHEN
        let request = builder.GET()
                        .url(URL(string: "http://api.somehost.com/test.json")!)
                        .query(name: "someQuery", value: "")
                        .build()
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    func testHostAndPath() {
        
        // GIVEN
        let url = URL(string: "https://api.somehost.com/some/path")
        let expectedRequest = URLRequest(url: url!)
        
        // WHEN
        let request = builder.host("api.somehost.com").path("some/path").build()
        
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    func testHeaderWithMultipleValues() {
        
        // GIVEN
        let url = URL(string: "https://api.somehost.com/some/path")
        var expectedRequest = URLRequest(url: url!)
        expectedRequest.setValue("text/html,application/xhtml+xml", forHTTPHeaderField: "Accept")
        
        // WHEN
        let request = builder
                        .host("api.somehost.com")
                        .path("some/path")
                        .setValue("text/html", for: "Accept")
                        .setValue("application/xhtml+xml", for: "Accept").build()
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    // MARK: - Build from JSON tests
    
    func testGetJSON() {
        
        // GIVEN
        let url = URL(string: "https://api.somehost.com/some/path")
        var expectedRequest = URLRequest(url: url!)
        expectedRequest.setValue("text/html,application/xhtml+xml", forHTTPHeaderField: "Accept")
        expectedRequest.setValue("en-IE", forHTTPHeaderField: "Accept-Language")
        
        // WHEN
        let fixtureURL = URL(fileURLWithPath: "./fixtures/requests.json")
        let data = try! Data(contentsOf: fixtureURL, options: [])
        let json = try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
        let request = builder.request("RequestGET", from: json).build()
        
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    func testPostJsonWithEmbeddedQueries() {
        
        // GIVEN
        let url = URL(string: "https://api.somehost.com/someResource?query=5&someOtherQuery=queryValue")
        var expectedRequest = URLRequest(url: url!)
        expectedRequest.httpMethod = "POST"
        
        // WHEN
        let fixtureURL = URL(fileURLWithPath: "./fixtures/requests.json")
        let data = try! Data(contentsOf: fixtureURL, options: [])
        let json = try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
        let request = builder.request("RequestPOST", from: json).build()
        
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    func testNonexistentRequestNameReturnsNil() {
        
        // GIVEN
        let requestName = "RequestNameWhichIsNotInOurJsonFile"
        
        // WHEN
        let fixtureURL = URL(fileURLWithPath: "./fixtures/requests.json")
        let data = try! Data(contentsOf: fixtureURL, options: [])
        let json = try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
        let request = builder.request(requestName, from: json).build()
        
        // THEN
        XCTAssertNil(request)
    }

    // MARK: - Linux
    
    static var allTests : [(String, (RequestBuilderTests) -> () throws -> Void)] {
        return [
            ("testRequestDefaultsToGET", testRequestDefaultsToGET),
            ("testRequestPOST", testRequestPOST)
        ]
    }
}
<commit_msg>Fixture helper for tests.<commit_after>import XCTest
import Foundation
@testable import RequestBuilder

class RequestBuilderTests: XCTestCase {
    
    // MARK: - Fixture path(s)
    let jsonFilePath = "./fixtures/requests.json"
    
    // MARK: - SUT
    
    let builder = RequestBuilder()
    
    // MARK: - Tests
    
    func testRequestDefaultsToGET() {
        
        // GIVEN
        let url = URL(string: "http://apple.com")
        let expectedRequest = URLRequest(url: url!)
        
        // WHEN
        let request = builder.url(url!).build()
        
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    func testRequestPOST() {
        
        // GIVEN
        let url = URL(string: "http://apple.com")
        var expectedRequest = URLRequest(url: url!)
        expectedRequest.httpMethod = "POST"
        
        // WHEN
        let request = builder.POST()
                        .url(url!)
                        .build()
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    func testRequestPUTAndHeaders() {
        
        // GIVEN
        let url = URL(string: "https://apple.com")
        var expectedRequest = URLRequest(url: url!)
        expectedRequest.httpMethod = "PUT"
        expectedRequest.setValue("Some token", forHTTPHeaderField: "Authorization")
        
        // WHEN
        let request = builder.PUT()
                        .url(url!)
                        .setValue("Some token", for: "Authorization")
                        .build()
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    func testRequestGETWithHeadersAndQueries() {
        
        // GIVEN
        let url = URL(string: "http://api.somehost.com/user_timeline.json?screen_name=cocoapatterns&include_rts=true")
        var expectedRequest = URLRequest(url: url!)
        expectedRequest.setValue("Some token", forHTTPHeaderField: "Authorization")
        
        // WHEN
        let request = builder.GET()
                        .url(URL(string: "http://api.somehost.com/user_timeline.json")!)
                        .setValue("Some token", for: "Authorization")
                        .query(name: "screen_name", value: "cocoapatterns")
                        .query(name: "include_rts", value: "true")
                        .build()
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    func testRequestGETWithEmptyQuery() {
        
        // GIVEN
        let url = URL(string: "http://api.somehost.com/test.json?someQuery=")
        let expectedRequest = URLRequest(url: url!)
    
        // WHEN
        let request = builder.GET()
                        .url(URL(string: "http://api.somehost.com/test.json")!)
                        .query(name: "someQuery", value: "")
                        .build()
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    func testHostAndPath() {
        
        // GIVEN
        let url = URL(string: "https://api.somehost.com/some/path")
        let expectedRequest = URLRequest(url: url!)
        
        // WHEN
        let request = builder.host("api.somehost.com").path("some/path").build()
        
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    func testHeaderWithMultipleValues() {
        
        // GIVEN
        let url = URL(string: "https://api.somehost.com/some/path")
        var expectedRequest = URLRequest(url: url!)
        expectedRequest.setValue("text/html,application/xhtml+xml", forHTTPHeaderField: "Accept")
        
        // WHEN
        let request = builder
                        .host("api.somehost.com")
                        .path("some/path")
                        .setValue("text/html", for: "Accept")
                        .setValue("application/xhtml+xml", for: "Accept").build()
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    // MARK: - Build from JSON tests
    
    func testGetJSON() {
        
        // GIVEN
        let url = URL(string: "https://api.somehost.com/some/path")
        var expectedRequest = URLRequest(url: url!)
        expectedRequest.setValue("text/html,application/xhtml+xml", forHTTPHeaderField: "Accept")
        expectedRequest.setValue("en-IE", forHTTPHeaderField: "Accept-Language")
        
        // WHEN
        let fixture = json(at: jsonFilePath)
        let request = builder.request("RequestGET", from: fixture).build()
        
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    func testPostJsonWithEmbeddedQueries() {
        
        // GIVEN
        let url = URL(string: "https://api.somehost.com/someResource?query=5&someOtherQuery=queryValue")
        var expectedRequest = URLRequest(url: url!)
        expectedRequest.httpMethod = "POST"
        
        // WHEN
        let fixture = json(at: jsonFilePath)
        let request = builder.request("RequestPOST", from: fixture).build()
        
        // THEN
        XCTAssertEqual(request, expectedRequest)
    }
    
    func testNonexistentRequestNameReturnsNil() {
        
        // GIVEN
        let requestName = "RequestNameWhichIsNotInOurJsonFile"
        
        // WHEN
        let fixture = json(at: jsonFilePath)
        let request = builder.request(requestName, from: fixture).build()
        
        // THEN
        XCTAssertNil(request)
    }
    
    // MARK: - Linux
    
    static var allTests : [(String, (RequestBuilderTests) -> () throws -> Void)] {
        return [
            ("testRequestDefaultsToGET", testRequestDefaultsToGET),
            ("testRequestPOST", testRequestPOST)
        ]
    }
}

// MARK: - Helpers

extension RequestBuilderTests {

    func json(at path: String) -> [String: Any] {
        let fixtureURL = URL(fileURLWithPath: path)
        let data = try! Data(contentsOf: fixtureURL, options: [])
        let json = try! JSONSerialization.jsonObject(with: data, options: []) as! [String: Any]
        return json
    }
}

{'fraction_non_alphanumeric': 0.1165654243966508, 'fraction_numerical': 0.00016417665407978985, 'mean_word_length': 1.7732756658320055, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 8, 'https://': 10, 'lorem ipsum': 0, 'www.': 0, 'xml': 6}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3743553', 'n_tokens_mistral': 3390, 'n_tokens_neox': 3309, 'n_words': 661}