Skip to content

Instantly share code, notes, and snippets.

@jarus
Created August 21, 2011 14:52
Show Gist options
  • Star 11 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jarus/1160696 to your computer and use it in GitHub Desktop.
Save jarus/1160696 to your computer and use it in GitHub Desktop.
Testing Flask application with basic authentication
import base64
from myapplication import app
class MyTestCase(unittest.TestCase):
def setUp(self):
self.app = app.test_client()
def tearDown(self):
pass
def open_with_auth(self, url, method, username, password):
return self.app.open(url,
method=method,
headers={
'Authorization': 'Basic ' + base64.b64encode(username + \
":" + password)
}
)
def test_login(self):
res = self.open_with_auth('/user/login', 'GET', 'username',
'password')
@gabrielfgularte
Copy link

Sorry budy, but this does not work for me: TypeError: 'str' does not support the buffer interface :(

Any ideas?

@markosski
Copy link

It's probably too late but seems like b64encode requires binary string - in this case you could do 'Authorization': 'Basic ' + base64.b64encode(bytes(username + ":" + password, 'ascii')).decode('ascii')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment