/usr/share/pyshared/mygpoclient/http_test.py is in python-mygpoclient 1.7-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | # -*- coding: utf-8 -*-
# gpodder.net API Client
# Copyright (C) 2009-2013 Thomas Perl and the gPodder Team
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from mygpoclient import http
import unittest
import multiprocessing
import BaseHTTPServer
def http_server(port, username, password, response):
storage = {}
class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
def __init__(self, *args, **kwargs):
BaseHTTPServer.BaseHTTPRequestHandler.__init__(self, *args, **kwargs)
def _checks(self):
if not self._check_auth():
return False
elif not self._check_errors():
return False
else:
return True
def _check_auth(self):
if self.path.startswith('/auth'):
authorization = self.headers.get('authorization', None)
if authorization is not None:
auth_type, credentials = authorization.split(None, 1)
if auth_type.lower() == 'basic':
auth_user, auth_pass = credentials.decode('base64').split(':', 1)
if username == auth_user and password == auth_pass:
return True
self.send_response(401)
self.send_header('WWW-Authenticate', 'Basic realm="Fake HTTP Server"')
self.end_headers()
self.wfile.close()
return False
return True
def _check_errors(self):
if self.path.startswith('/badrequest'):
self.send_response(400)
self.end_headers()
self.wfile.close()
return False
elif self.path.startswith('/notfound'):
self.send_response(404)
self.end_headers()
self.wfile.close()
return False
elif self.path.startswith('/invaliderror'):
self.send_response(444)
self.end_headers()
self.wfile.close()
return False
return True
def do_POST(self):
if not self._checks():
return
input_data = self.rfile.read(int(self.headers.get('content-length')))
self.send_response(200)
self.end_headers()
self.wfile.write(input_data.encode('rot13'))
self.wfile.close()
def do_PUT(self):
if not self._checks():
return
input_data = self.rfile.read(int(self.headers.get('content-length')))
storage[self.path] = input_data
self.send_response(200)
self.end_headers()
self.wfile.write('PUT OK')
self.wfile.close()
def do_GET(self):
if not self._checks():
return
self.send_response(200)
self.end_headers()
if self.path in storage:
self.wfile.write(storage[self.path])
else:
self.wfile.write(response)
self.wfile.close()
def log_request(*args):
pass
BaseHTTPServer.HTTPServer(('127.0.0.1', port), Handler).serve_forever()
class Test_HttpClient(unittest.TestCase):
USERNAME = 'john'
PASSWORD = 'secret'
PORT = 9876
URI_BASE = 'http://localhost:%(PORT)d' % locals()
RESPONSE = 'Test_GET-HTTP-Response-Content'
DUMMYDATA = 'fq28cnyp3ya8ltcy;ny2t8ay;iweuycowtc'
def setUp(self):
self.server_process = multiprocessing.Process(target=http_server,
args=(self.PORT, self.USERNAME, self.PASSWORD, self.RESPONSE))
self.server_process.start()
import time
time.sleep(.1)
def tearDown(self):
self.server_process.terminate()
import time
time.sleep(.1)
def test_UnknownResponse(self):
client = http.HttpClient()
path = self.URI_BASE+'/invaliderror'
self.assertRaises(http.UnknownResponse, client.GET, path)
def test_NotFound(self):
client = http.HttpClient()
path = self.URI_BASE+'/notfound'
self.assertRaises(http.NotFound, client.GET, path)
def test_Unauthorized(self):
client = http.HttpClient('invalid-username', 'invalid-password')
path = self.URI_BASE+'/auth'
self.assertRaises(http.Unauthorized, client.GET, path)
def test_BadRequest(self):
client = http.HttpClient()
path = self.URI_BASE+'/badrequest'
self.assertRaises(http.BadRequest, client.GET, path)
def test_GET(self):
client = http.HttpClient()
path = self.URI_BASE+'/noauth'
self.assertEquals(client.GET(path), self.RESPONSE)
def test_authenticated_GET(self):
client = http.HttpClient(self.USERNAME, self.PASSWORD)
path = self.URI_BASE+'/auth'
self.assertEquals(client.GET(path), self.RESPONSE)
def test_unauthenticated_GET(self):
client = http.HttpClient()
path = self.URI_BASE+'/auth'
self.assertRaises(http.Unauthorized, client.GET, path)
def test_POST(self):
client = http.HttpClient()
path = self.URI_BASE+'/noauth'
self.assertEquals(client.POST(path, self.DUMMYDATA), self.DUMMYDATA.encode('rot13'))
def test_authenticated_POST(self):
client = http.HttpClient(self.USERNAME, self.PASSWORD)
path = self.URI_BASE+'/auth'
self.assertEquals(client.POST(path, self.DUMMYDATA), self.DUMMYDATA.encode('rot13'))
def test_unauthenticated_POST(self):
client = http.HttpClient()
path = self.URI_BASE+'/auth'
self.assertRaises(http.Unauthorized, client.POST, path, self.DUMMYDATA)
def test_PUT(self):
client = http.HttpClient()
path = self.URI_BASE+'/noauth'
self.assertEquals(client.PUT(path, self.DUMMYDATA), 'PUT OK')
def test_GET_after_PUT(self):
client = http.HttpClient()
for i in range(10):
path = self.URI_BASE + '/file.%(i)d.txt' % locals()
client.PUT(path, self.RESPONSE + str(i))
self.assertEquals(client.GET(path), self.RESPONSE + str(i))
|