Skip to content

Commit e1149d0

Browse files
authored
Merge pull request #2740 from apple/revert-2734-pr_http_server_enhancements
Revert "Test HTTPServer with urls that match the method"
2 parents 5031667 + eaa71f4 commit e1149d0

File tree

1 file changed

+12
-75
lines changed

1 file changed

+12
-75
lines changed

Tests/Foundation/HTTPServer.swift

Lines changed: 12 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -483,11 +483,9 @@ class _HTTPServer {
483483

484484
struct _HTTPRequest {
485485
enum Method : String {
486-
case HEAD
487486
case GET
488487
case POST
489488
case PUT
490-
case DELETE
491489
}
492490
let method: Method
493491
let uri: String
@@ -497,22 +495,14 @@ struct _HTTPRequest {
497495
let headers: [String]
498496

499497
enum Error: Swift.Error {
500-
case invalidURI
501-
case invalidMethod
502498
case headerEndNotFound
503499
}
504500

505501
public init(header: String) throws {
506502
headers = header.components(separatedBy: _HTTPUtils.CRLF)
507-
guard headers.count > 0 else {
508-
throw Error.invalidURI
509-
}
510-
let uriParts = headers[0].components(separatedBy: " ")
511-
guard uriParts.count > 2, let methodName = Method(rawValue: uriParts[0]) else {
512-
throw Error.invalidMethod
513-
}
514-
method = methodName
515-
uri = uriParts[1]
503+
let action = headers[0]
504+
method = Method(rawValue: action.components(separatedBy: " ")[0])!
505+
uri = action.components(separatedBy: " ")[1]
516506
body = ""
517507
}
518508

@@ -534,31 +524,14 @@ struct _HTTPRequest {
534524
}
535525
return nil
536526
}
537-
538-
public func headersAsJSON() throws -> Data {
539-
var headerDict: [String: String] = [:]
540-
for header in headers {
541-
if header.hasPrefix(method.rawValue) {
542-
headerDict["uri"] = header
543-
continue
544-
}
545-
let parts = header.components(separatedBy: ":")
546-
if parts.count > 1 {
547-
headerDict[parts[0]] = parts[1].trimmingCharacters(in: CharacterSet(charactersIn: " "))
548-
}
549-
}
550-
return try JSONEncoder().encode(headerDict)
551-
}
552527
}
553528

554529
struct _HTTPResponse {
555530
enum Response: Int {
556531
case OK = 200
557532
case REDIRECT = 302
558-
case BAD_REQUEST = 400
559533
case NOTFOUND = 404
560534
case METHOD_NOT_ALLOWED = 405
561-
case SERVER_ERROR = 500
562535
}
563536
private let responseCode: Response
564537
private let headers: String
@@ -627,56 +600,20 @@ public class TestURLSessionServer {
627600
} else if req.uri.hasPrefix("/unauthorized") {
628601
try httpServer.respondWithUnauthorizedHeader()
629602
} else {
630-
try httpServer.respond(with: getResponse(request: req))
631-
}
632-
}
633-
634-
func getResponse(request: _HTTPRequest) throws -> _HTTPResponse {
635-
636-
func headersAsJSONResponse() throws -> _HTTPResponse {
637-
guard let body = try String(data: request.headersAsJSON(), encoding: .utf8) else {
638-
return _HTTPResponse(response: .SERVER_ERROR, body: "Cant convert headers to JSON object")
603+
if req.method == .GET || req.method == .POST || req.method == .PUT {
604+
try httpServer.respond(with: getResponse(request: req))
605+
}
606+
else {
607+
try httpServer.respond(with: _HTTPResponse(response: .METHOD_NOT_ALLOWED, body: "Method not allowed"))
639608
}
640-
let headers = "Content-Type: application/json\r\nContent-Length: \(body.count)"
641-
return _HTTPResponse(response: .OK, headers: headers, body: body)
642609
}
610+
}
643611

612+
func getResponse(request: _HTTPRequest) -> _HTTPResponse {
644613
let uri = request.uri
645-
if uri == "/head" {
646-
guard request.method == .HEAD else { return _HTTPResponse(response: .METHOD_NOT_ALLOWED, body: "Method not allowed") }
647-
return try headersAsJSONResponse()
648-
}
649-
650-
if uri == "/get" {
651-
guard request.method == .GET else { return _HTTPResponse(response: .METHOD_NOT_ALLOWED, body: "Method not allowed") }
652-
return try headersAsJSONResponse()
653-
}
654-
655-
if uri == "/put" {
656-
guard request.method == .PUT else { return _HTTPResponse(response: .METHOD_NOT_ALLOWED, body: "Method not allowed") }
657-
return try headersAsJSONResponse()
658-
}
659-
660-
if uri == "/post" {
661-
guard request.method == .POST else { return _HTTPResponse(response: .METHOD_NOT_ALLOWED, body: "Method not allowed") }
662-
return try headersAsJSONResponse()
663-
}
664-
665-
if uri == "/delete" {
666-
guard request.method == .DELETE else { return _HTTPResponse(response: .METHOD_NOT_ALLOWED, body: "Method not allowed") }
667-
return try headersAsJSONResponse()
668-
}
669-
670614
if uri == "/upload" {
671-
if let contentLength = request.getHeader(for: "content-length") {
672-
let text = "Upload completed!, Content-Length: \(contentLength)"
673-
return _HTTPResponse(response: .OK, headers: "Content-Length: \(text.data(using: .utf8)!.count)", body: text)
674-
}
675-
if let te = request.getHeader(for: "transfer-encoding"), te == "chunked" {
676-
return _HTTPResponse(response: .OK, body: "Received Chunked request")
677-
} else {
678-
return _HTTPResponse(response: .BAD_REQUEST, body: "Missing Content-Length")
679-
}
615+
let text = "Upload completed!"
616+
return _HTTPResponse(response: .OK, headers: "Content-Length: \(text.data(using: .utf8)!.count)", body: text)
680617
}
681618

682619
if uri == "/country.txt" {

0 commit comments

Comments
 (0)