mirror of
https://github.com/kennethreitz/httpbin.git
synced 2026-06-05 23:00:18 +00:00
base64-decoding endpoint + test
This commit is contained in:
@@ -7,6 +7,7 @@ httpbin.core
|
||||
This module provides the core HttpBin experience.
|
||||
"""
|
||||
|
||||
import base64
|
||||
import json
|
||||
import os
|
||||
import time
|
||||
@@ -241,6 +242,12 @@ def digest_auth(qop=None, user='user', passwd='passwd'):
|
||||
return dict(authenticated=True, user=user)
|
||||
|
||||
|
||||
@app.route('/base64/<value>')
|
||||
def decode_base64(value):
|
||||
"""Decodes base64url-encoded string"""
|
||||
encoded = value.encode('utf-8')
|
||||
return base64.urlsafe_b64decode(encoded).decode('utf-8')
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
app.run()
|
||||
|
||||
Executable
+30
@@ -0,0 +1,30 @@
|
||||
#!/usr/bin/env python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
import httpbin
|
||||
import unittest
|
||||
import base64
|
||||
|
||||
|
||||
def _string_to_base64(string):
|
||||
"""Encodes string to utf-8 and then base64"""
|
||||
utf8_encoded = string.encode('utf-8')
|
||||
return base64.urlsafe_b64encode(utf8_encoded)
|
||||
|
||||
|
||||
class HttpbinTestCase(unittest.TestCase):
|
||||
"""Httpbin tests"""
|
||||
|
||||
def setUp(self):
|
||||
self.app = httpbin.app.test_client()
|
||||
|
||||
def test_base64(self):
|
||||
greeting = u'Здравствуй, мир!'
|
||||
b64_encoded = _string_to_base64(greeting)
|
||||
response = self.app.get('/base64/{}'.format(b64_encoded))
|
||||
content = response.data.decode('utf-8')
|
||||
self.assertEquals(greeting, content)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user