<commit_before>/* *************************************************************************************************
 ClientTests.swift
   © 2018, 2020 YOCKOW.
     Licensed under MIT License.
     See "LICENSE.txt" for more information.
 ************************************************************************************************ */

import XCTest
@testable import CGIResponder

import Foundation
import TemporaryFile

final class ClientTests: XCTestCase {
  func test_cookies() {
    let cookies = "name1=value1; name2=value2; name1=another_value1"
    let items = Client.virtual(environmentVariables: .virtual(["HTTP_COOKIE": cookies])).request.cookies()
    XCTAssertNotNil(items)
    XCTAssertEqual(items?[0].name, "name1")
    XCTAssertEqual(items?[0].value, "value1")
    XCTAssertEqual(items?[1].name, "name2")
    XCTAssertEqual(items?[1].value, "value2")
    XCTAssertEqual(items?[2].name, "name1")
    XCTAssertEqual(items?[2].value, "another_value1")
  }
  
  func test_queryItems() {
    let urlQuery = "name1=value1&name2=value2"
    let postData = "name3=value3&name4".data(using:.utf8)!
    let contentType = ContentType(type:.application, subtype:"x-www-form-urlencoded")!
    let contentLength = postData.count
    
    let env: [String:String] = [
      "QUERY_STRING": urlQuery,
      "CONTENT_TYPE": contentType.description,
      "CONTENT_LENGTH": String(contentLength),
      "REQUEST_METHOD": "POST",
    ]
    
    let client = Client.virtual(standardInput: InMemoryFile(postData),
                                environmentVariables: .virtual(env))
    let queryItems = client.request.queryItems
    XCTAssertNotNil(queryItems)
    XCTAssertEqual(queryItems?.first(where: { $0.name == "name1" })?.value, "value1")
    XCTAssertEqual(queryItems?.first(where: { $0.name == "name2" })?.value, "value2")
    XCTAssertEqual(queryItems?.first(where: { $0.name == "name3" })?.value, "value3")
    XCTAssertEqual(queryItems?.first(where: { $0.name == "name4" })?.value, nil)
    XCTAssertNotNil(queryItems?.first(where: { $0.name == "name4" }))
  }
  
  func test_queryString() {
    var client = Client.virtual(environmentVariables: .virtual(["QUERY_STRING":"q"]))
    XCTAssertEqual(client.request.queryString, "q")
    
    client = Client.virtual(environmentVariables: .virtual(["QUERY_STRING":"", "REQUEST_URI": "/"]))
    XCTAssertEqual(client.request.queryString, nil)
    
    client = Client.virtual(environmentVariables: .virtual(["QUERY_STRING":"", "REQUEST_URI": "/?"]))
    XCTAssertEqual(client.request.queryString, "")
  }
  
  func test_formDataItems() throws {<commit_msg>Add workaround for https://github.com/YOCKOW/SwiftCGIResponder/issues/69 and https://bugs.swift.org/browse/SR-14620<commit_after>/* *************************************************************************************************
 ClientTests.swift
   © 2018, 2020 YOCKOW.
     Licensed under MIT License.
     See "LICENSE.txt" for more information.
 ************************************************************************************************ */

import XCTest
@testable import CGIResponder

import Foundation
import TemporaryFile

final class ClientTests: XCTestCase {
  func test_cookies() {
    let cookies = "name1=value1; name2=value2; name1=another_value1"
    let items = Client.virtual(environmentVariables: .virtual(["HTTP_COOKIE": cookies])).request.cookies()
    XCTAssertNotNil(items)
    XCTAssertEqual(items?[0].name, "name1")
    XCTAssertEqual(items?[0].value, "value1")
    XCTAssertEqual(items?[1].name, "name2")
    XCTAssertEqual(items?[1].value, "value2")
    XCTAssertEqual(items?[2].name, "name1")
    XCTAssertEqual(items?[2].value, "another_value1")
  }
  
  func test_queryItems() throws {
    let urlQuery = "name1=value1&name2=value2"
    let postData = "name3=value3&name4".data(using:.utf8)!
    let contentType = ContentType(type:.application, subtype:"x-www-form-urlencoded")!
    let contentLength = postData.count
    
    let env: [String:String] = [
      "QUERY_STRING": urlQuery,
      "CONTENT_TYPE": contentType.description,
      "CONTENT_LENGTH": String(contentLength),
      "REQUEST_METHOD": "POST",
    ]

    /*
      Requires workaround for
        https://github.com/YOCKOW/SwiftCGIResponder/issues/69
        https://bugs.swift.org/browse/SR-14620
    */
    
    let client = Client.virtual(standardInput: InMemoryFile(postData),
                                environmentVariables: .virtual(env))
    let queryItems = try XCTUnwrap(client.request.queryItems)
    XCTAssertEqual(queryItems.first(where: { $0.name == "name1" })?.value, "value1")
    XCTAssertEqual(queryItems.first(where: { $0.name == "name2" })?.value, "value2")
    XCTAssertEqual(queryItems.first(where: { $0.name == "name3" })?.value, "value3")
    XCTAssertEqual(queryItems.first(where: { $0.name == "name4" })?.value, nil)
    XCTAssertNotNil(queryItems.first(where: { $0.name == "name4" }))
  }
  
  func test_queryString() {
    var client = Client.virtual(environmentVariables: .virtual(["QUERY_STRING":"q"]))
    XCTAssertEqual(client.request.queryString, "q")
    
    client = Client.virtual(environmentVariables: .virtual(["QUERY_STRING":"", "REQUEST_URI": "/"]))
    XCTAssertEqual(client.request.queryString, nil)
    
    client = Client.virtual(environmentVariables: .virtual(["QUERY_STRING":"", "REQUEST_URI": "/?"]))
    XCTAssertEqual(client.request.queryString, "")
  }
  
  func test_formDataItems() throws {
{'fraction_non_alphanumeric': 0.20951509606587373, 'fraction_numerical': 0.019762122598353155, 'mean_word_length': 4.493467336683417, 'pattern_counts': {'":': 20, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 4, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, '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': '7137580', 'n_tokens_mistral': 1762, 'n_tokens_neox': 1690, 'n_words': 279}