def load(self, arguments): """ Looks through the arguments to find directives specifying the message (ie, -M, --stdin, etc). If none are found, the defaultRequest/ defaultResponse are used. """ request = arguments.messageType == REQUEST type = "request" if request else "response" if arguments.message: if request: message = parseRequest(arguments.message) else: message = parseResponse(arguments.message) elif arguments.stdin: info( "Reading {} from stdin".format(type), arguments.verbose ) if request: message = readRequest() else: message = self.readResponse() elif arguments.file: info( "Reading {} from {}".format(type, arguments.file), arguments.verbose ) f = open(arguments.file, "r") if request: message = readRequest(f) else: message = self.readResponse(f) else: if request: message = self.defaultRequest else: message = self.defaultResponse return message
def testParsingResponsesWithSpaceInReason(self): r = "\r\n".join(( "HTTP/1.1 404 NOT FOUND", "Content-type: text/plain", "", "No idea." )) self.assertEqual( parseResponse(r), { "version": "HTTP/1.1", "status": "404", "reason": "NOT FOUND", "headers": ( ("Content-type", "text/plain"), ), "entity": "No idea." } )
def testParsingResponses(self): r = "\r\n".join(( "HTTP/1.1 200 OK", "Content-type: text/plain", "heaDer: vAlue", "", "B" )) self.assertEqual( parseResponse(r), { "version": "HTTP/1.1", "status": "200", "reason": "OK", "entity": "B", "headers": ( ("Content-type", "text/plain"), ("heaDer", "vAlue") ) } )
def readResponse(file=stdin): """ Reads an entire file & parses it as a response. """ return parseResponse(file.read())