|
| 1 | +''' |
| 2 | +Test cookie rewrite |
| 3 | +''' |
| 4 | +import os |
| 5 | +import requests |
| 6 | +import time |
| 7 | +import logging |
| 8 | +import random |
| 9 | +import tsqa.test_cases |
| 10 | +import helpers |
| 11 | +import shutil |
| 12 | +import SocketServer |
| 13 | +import urllib2 |
| 14 | + |
| 15 | +log = logging.getLogger(__name__) |
| 16 | + |
| 17 | +class EchoServerHandler(SocketServer.BaseRequestHandler): |
| 18 | + """ |
| 19 | + A subclass of RequestHandler which will return all data received back |
| 20 | + """ |
| 21 | + |
| 22 | + def handle(self): |
| 23 | + # Receive the data in small chunks and retransmit it |
| 24 | + while True: |
| 25 | + data = self.request.recv(4096).strip() |
| 26 | + if data: |
| 27 | + log.debug('Sending data back to the client') |
| 28 | + else: |
| 29 | + log.debug('Client disconnected') |
| 30 | + break |
| 31 | + cookie = '' |
| 32 | + if 'Cookie' in data: |
| 33 | + cookie = data.split('Cookie: ')[1].split('\r\n')[0] |
| 34 | + |
| 35 | + resp = ('HTTP/1.1 200 OK\r\n' |
| 36 | + 'Content-Length: {data_length}\r\n' |
| 37 | + 'Content-Type: text/html; charset=UTF-8\r\n' |
| 38 | + 'Connection: keep-alive\r\n' |
| 39 | + '\r\n{data_string}'.format( |
| 40 | + data_length = len(cookie), |
| 41 | + data_string = cookie |
| 42 | + )) |
| 43 | + self.request.sendall(resp) |
| 44 | + |
| 45 | +class TestHeaderRewrite(helpers.EnvironmentCase): |
| 46 | + ''' |
| 47 | + Tests for header rewrite |
| 48 | + ''' |
| 49 | + @classmethod |
| 50 | + def setUpEnv(cls, env): |
| 51 | + cls.traffic_server_port = int(cls.configs['records.config']['CONFIG']['proxy.config.http.server_ports']) |
| 52 | + |
| 53 | + # create a socket server |
| 54 | + cls.socket_server = tsqa.endpoint.SocketServerDaemon(EchoServerHandler) |
| 55 | + cls.socket_server.start() |
| 56 | + cls.socket_server.ready.wait() |
| 57 | + |
| 58 | + cls.configs['remap.config'].add_line( |
| 59 | + 'map / http://127.0.0.1:%d' %(cls.socket_server.port) |
| 60 | + ) |
| 61 | + |
| 62 | + # setup the plugin |
| 63 | + cls.config_file = 'header-rewrite.config' |
| 64 | + cls.test_config_path = helpers.tests_file_path(cls.config_file) |
| 65 | + |
| 66 | + cls.configs['plugin.config'].add_line('%s/header_rewrite.so %s' % ( |
| 67 | + cls.environment.layout.plugindir, |
| 68 | + cls.test_config_path |
| 69 | + )) |
| 70 | + |
| 71 | + def test_cookie_rewrite(self): |
| 72 | + |
| 73 | + cookie_test_add_dict = { |
| 74 | + '' : 'testkey=testaddvalue', |
| 75 | + 'testkey=somevalue' : 'testkey=somevalue', |
| 76 | + 'otherkey=testvalue' : 'otherkey=testvalue;testkey=testaddvalue', |
| 77 | + 'testkey = "other=value"; a = a' : 'testkey = "other=value"; a = a', |
| 78 | + 'testkeyx===' : 'testkeyx===;testkey=testaddvalue' |
| 79 | + } |
| 80 | + for key in cookie_test_add_dict: |
| 81 | + opener = urllib2.build_opener() |
| 82 | + opener.addheaders.append(('Cookie', key)) |
| 83 | + f = opener.open("http://127.0.0.1:%d/addcookie" % (self.traffic_server_port)) |
| 84 | + resp = f.read() |
| 85 | + self.assertEqual(resp, cookie_test_add_dict[key]) |
| 86 | + |
| 87 | + cookie_test_rm_dict = { |
| 88 | + '' : '', |
| 89 | + ' testkey=somevalue' : '', |
| 90 | + 'otherkey=testvalue' : 'otherkey=testvalue', |
| 91 | + 'testkey = "other=value" ; a = a' : ' a = a', |
| 92 | + 'otherkey=othervalue= ; testkey===' : 'otherkey=othervalue= ', |
| 93 | + 'firstkey ="firstvalue" ; testkey = =; secondkey=\'\'' : 'firstkey ="firstvalue" ; secondkey=\'\'' |
| 94 | + } |
| 95 | + for key in cookie_test_rm_dict: |
| 96 | + opener = urllib2.build_opener() |
| 97 | + opener.addheaders.append(('Cookie', key)) |
| 98 | + f = opener.open("http://127.0.0.1:%d/rmcookie" % (self.traffic_server_port)) |
| 99 | + resp = f.read() |
| 100 | + self.assertEqual(resp, cookie_test_rm_dict[key]) |
| 101 | + |
| 102 | + cookie_test_set_dict = { |
| 103 | + '' : 'testkey=testsetvalue', |
| 104 | + 'testkey=somevalue' : 'testkey=testsetvalue', |
| 105 | + 'otherkey=testvalue' : 'otherkey=testvalue;testkey=testsetvalue', |
| 106 | + 'testkey = "other=value"; a = a' : 'testkey = testsetvalue; a = a', |
| 107 | + 'testkeyx===' : 'testkeyx===;testkey=testsetvalue', |
| 108 | + 'firstkey ="firstvalue" ; testkey = =; secondkey=\'\'' : 'firstkey ="firstvalue" ; testkey = testsetvalue; secondkey=\'\'' |
| 109 | + } |
| 110 | + for key in cookie_test_set_dict: |
| 111 | + opener = urllib2.build_opener() |
| 112 | + opener.addheaders.append(('Cookie', key)) |
| 113 | + f = opener.open("http://127.0.0.1:%d/setcookie" % (self.traffic_server_port)) |
| 114 | + resp = f.read() |
| 115 | + self.assertEqual(resp, cookie_test_set_dict[key]) |
0 commit comments