Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support POST for basic authentication #607

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion httpbin/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ def delete_cookies():
return r


@app.route("/basic-auth/<user>/<passwd>")
@app.route("/basic-auth/<user>/<passwd>", methods=["GET", "POST"])
def basic_auth(user="user", passwd="passwd"):
"""Prompts the user for authorization using HTTP Basic Auth.
---
Expand Down
19 changes: 16 additions & 3 deletions test_httpbin.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ def test_get(self):
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['args'], {})
self.assertEqual(data['headers']['Host'], 'localhost')
self.assertEqual(data['headers']['Content-Length'], '0')
self.assertEqual(data['headers']['User-Agent'], 'test')
# self.assertEqual(data['origin'], None)
self.assertEqual(data['url'], 'http://localhost/get')
self.assertTrue(response.data.endswith(b'\n'))

Expand All @@ -162,7 +160,6 @@ def test_anything(self):
data = json.loads(response.data.decode('utf-8'))
self.assertEqual(data['args'], {})
self.assertEqual(data['headers']['Host'], 'localhost')
self.assertEqual(data['headers']['Content-Length'], '0')
self.assertEqual(data['url'], 'http://localhost/anything/foo/bar')
self.assertEqual(data['method'], 'GET')
self.assertTrue(response.data.endswith(b'\n'))
Expand Down Expand Up @@ -308,6 +305,22 @@ def test_brotli(self):
response = self.app.get('/brotli')
self.assertEqual(response.status_code, 200)

def test_basic_auth_get_when_basic_auth_credentials_set_should_return_ok(self):
response = self.app.get('/basic-auth/foo/bar', headers={'Authorization': 'Basic Zm9vOmJhcg=='})
self.assertEqual(response.status_code, 200)

def test_basic_auth_get_when_no_basic_auth_credentials_set_should_return_unauthorized(self):
response = self.app.get('/basic-auth/foo/bar')
self.assertEqual(response.status_code, 401)

def test_basic_auth_post_when_basic_auth_credentials_set_should_return_ok(self):
response = self.app.get('/basic-auth/foo/bar', headers={'Authorization': 'Basic Zm9vOmJhcg=='})
self.assertEqual(response.status_code, 200)

def test_basic_auth_post_when_no_basic_auth_credentials_set_should_return_unauthorized(self):
response = self.app.post('/basic-auth/foo/bar')
self.assertEqual(response.status_code, 401)

def test_bearer_auth(self):
token = 'abcd1234'
response = self.app.get(
Expand Down