<commit_before>/***************************************************************************************************
 CGIResponderTests.swift
   © 2017 YOCKOW.
     Licensed under MIT License.
     See "LICENSE.txt" for more information.
 **************************************************************************************************/

import XCTest
@testable import CGIResponder
import Foundation

class CGIResponderTests: XCTestCase {
  func testContentType() {
    var responder = CGIResponder()
    XCTAssertEqual(responder.contentType, MIMEType(type:.application, subtype:"octet-stream"))
    XCTAssertEqual(responder.stringEncoding, nil)
    
    responder.contentType = MIMEType(pathExtension:.txt, parameters:["charset":"UTF-8"])!
    XCTAssertEqual(responder.contentType.type, .text)
    XCTAssertEqual(responder.contentType.subtype, "plain")
    XCTAssertEqual(responder.stringEncoding, .utf8)
  }
  
  func testOutput() {
    var output = Data()
    var responder = CGIResponder()
    responder.status = .ok
    responder.contentType = MIMEType(pathExtension:.txt, parameters:["charset":"UTF-8"])!
    responder.content = .string("Hello, World!\n", encoding:.utf8)
    
    let expected = "Status: \(HTTPStatusCode.ok.rawValue) \(HTTPStatusCode.ok.reasonPhrase)\r\n" +
      "\(HTTPHeaderFieldName.contentType.rawValue): \(MIMEType(pathExtension:.txt, parameters:["charset":"UTF-8"])!.description)\r\n" +
      "\r\n" +
      "Hello, World!\n"
    
    XCTAssertNoThrow(try responder.respond(to:&output))
    XCTAssertEqual(String(data:output, encoding:.utf8), expected)
  }
  
  func testStringEncoding() {
    var output = FileHandle.nullDevice
    var responder = CGIResponder()
    responder.contentType = MIMEType(pathExtension:.txt, parameters:["charset":"US-ASCII"])!
    responder.content = .string("?", encoding:.utf8)
    
    let path = NSTemporaryDirectory() + UUID().uuidString
    FileManager.default.createFile(atPath:path, contents:nil, attributes:nil)
    let tmpFile = FileHandle(forUpdatingAtPath:path)!
    FileHandle.changeableStandardError = tmpFile
    XCTAssertNoThrow(try responder.respond(to:&output))
    FileHandle.changeableStandardError = FileHandle.standardError
    
    tmpFile.seek(toFileOffset:0)
    let data = tmpFile.availableData
    tmpFile.closeFile()
    
    let message = String(data:data, encoding:.utf8)!.trimmingCharacters(in:.whitespacesAndNewlines)
    
    XCTAssertEqual(ErrorMessage.stringEncodingInconsistency(.utf8, String.Encoding(ianaCharacterSetName:"US-ASCII")!).rawValue,
                   message)
    
  }
  
  static var allTests: [(String, (CGIResponderTests) -> () -> Void)] = [
    ("testContentType", testContentType),
    ("testOutput", testOutput),
    ("testStringEncoding", testStringEncoding),
  ]
}
<commit_msg>Check the result of `FileManager.default.createFile(atPath:contents:attributes:)`.<commit_after>/***************************************************************************************************
 CGIResponderTests.swift
   © 2017 YOCKOW.
     Licensed under MIT License.
     See "LICENSE.txt" for more information.
 **************************************************************************************************/

import XCTest
@testable import CGIResponder
import Foundation

class CGIResponderTests: XCTestCase {
  func testContentType() {
    var responder = CGIResponder()
    XCTAssertEqual(responder.contentType, MIMEType(type:.application, subtype:"octet-stream"))
    XCTAssertEqual(responder.stringEncoding, nil)
    
    responder.contentType = MIMEType(pathExtension:.txt, parameters:["charset":"UTF-8"])!
    XCTAssertEqual(responder.contentType.type, .text)
    XCTAssertEqual(responder.contentType.subtype, "plain")
    XCTAssertEqual(responder.stringEncoding, .utf8)
  }
  
  func testOutput() {
    var output = Data()
    var responder = CGIResponder()
    responder.status = .ok
    responder.contentType = MIMEType(pathExtension:.txt, parameters:["charset":"UTF-8"])!
    responder.content = .string("Hello, World!\n", encoding:.utf8)
    
    let expected = "Status: \(HTTPStatusCode.ok.rawValue) \(HTTPStatusCode.ok.reasonPhrase)\r\n" +
      "\(HTTPHeaderFieldName.contentType.rawValue): \(MIMEType(pathExtension:.txt, parameters:["charset":"UTF-8"])!.description)\r\n" +
      "\r\n" +
      "Hello, World!\n"
    
    XCTAssertNoThrow(try responder.respond(to:&output))
    XCTAssertEqual(String(data:output, encoding:.utf8), expected)
  }
  
  func testStringEncoding() {
    var output = FileHandle.nullDevice
    var responder = CGIResponder()
    responder.contentType = MIMEType(pathExtension:.txt, parameters:["charset":"US-ASCII"])!
    responder.content = .string("?", encoding:.utf8)
    
    let path = NSTemporaryDirectory() + UUID().uuidString
    XCTAssertTrue(FileManager.default.createFile(atPath:path, contents:nil, attributes:nil))
    
    let tmpFile = FileHandle(forUpdatingAtPath:path)!
    FileHandle.changeableStandardError = tmpFile
    XCTAssertNoThrow(try responder.respond(to:&output))
    FileHandle.changeableStandardError = FileHandle.standardError
    
    tmpFile.seek(toFileOffset:0)
    let data = tmpFile.availableData
    tmpFile.closeFile()
    
    let message = String(data:data, encoding:.utf8)!.trimmingCharacters(in:.whitespacesAndNewlines)
    
    XCTAssertEqual(ErrorMessage.stringEncodingInconsistency(.utf8, String.Encoding(ianaCharacterSetName:"US-ASCII")!).rawValue,
                   message)
    
  }
  
  static var allTests: [(String, (CGIResponderTests) -> () -> Void)] = [
    ("testContentType", testContentType),
    ("testOutput", testOutput),
    ("testStringEncoding", testStringEncoding),
  ]
}

{'fraction_non_alphanumeric': 0.18962181178540016, 'fraction_numerical': 0.004925241864555849, 'mean_word_length': 4.646474677259186, 'pattern_counts': {'":': 8, '<': 3, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '7383481', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1657, 'n_words': 264}