configure mock server passing 0 as the port will cause a random free port to be chosen self mockserver httpserver localhost 0 handler self mockserverport self mockserver serveraddress start running mock server in a separate thread daemon threads automatically shut down when the main process exits self mockserverthread threadtargetself mockserver serveforever self mockserverthread setdaemontrue self mockserverthread start def testcookieparserself class mockserverrequesthandlerbasehttprequesthandler handle get requests craft multiple cookies cookie simplecookie cookie hello world cookie hello path self path cookie oatmealraisin is the best cookie oatmealraisin path self path send http headers self sendresponse200 self sendheader setcookie cookie output self endheaders self setupmockservermockserverrequesthandler response httpf http localhost self mockserverport assert setcookie helloworld path in response assert setcookie oatmealraisinis the best path in response configure mock server passing 0 as the port will cause a random free port to be chosen start running mock server in a separate thread daemon threads automatically shut down when the main process exits not directly testing httpie but requests to ensure their cookies handling is still as expected by get_expired_cookies http request handler handle get requests craft multiple cookies send http headers
from http.cookies import SimpleCookie
from http.server import BaseHTTPRequestHandler, HTTPServer
from threading import Thread

from .utils import http


class TestIntegration:

    def setup_mock_server(self, handler):
        
        
        self.mock_server = HTTPServer(('localhost', 0), handler)
        _, self.mock_server_port = self.mock_server.server_address

        
        
        self.mock_server_thread = Thread(target=self.mock_server.serve_forever)
        self.mock_server_thread.setDaemon(True)
        self.mock_server_thread.start()

    def test_cookie_parser(self):
        

        class MockServerRequestHandler(BaseHTTPRequestHandler):
            

            def do_GET(self):
                
                
                cookie = SimpleCookie()
                cookie['hello'] = 'world'
                cookie['hello']['path'] = self.path
                cookie['oatmeal_raisin'] = 'is the best'
                cookie['oatmeal_raisin']['path'] = self.path

                
                self.send_response(200)
                self.send_header('Set-Cookie', cookie.output())
                self.end_headers()

        self.setup_mock_server(MockServerRequestHandler)
        response = http(f'http://localhost:{self.mock_server_port}/')
        assert 'Set-Cookie: hello=world; Path=/' in response
        assert 'Set-Cookie: oatmeal_raisin="is the best"; Path=/' in response