EMF_Camp_Badge/lib/test_http.py

49 lines
1.8 KiB
Python
Raw Normal View History

"""Tests for http"""
2018-07-16 18:04:42 -04:00
___license___ = "MIT"
___dependencies___ = ["upip:unittest", "http", "wifi"]
2018-07-16 18:04:42 -04:00
import unittest
from http import *
import wifi
2018-07-16 18:04:42 -04:00
class TestHttp(unittest.TestCase):
def setUpClass(self):
wifi.connect()
def test_get_with_https(self):
2018-08-25 11:52:30 -04:00
with get("https://httpbin.org/get") as response:
self.assertEqual(response.status, 200)
print(response.text)
def test_get(self):
with get("http://httpbin.org/get", params={"foo": "bar"}, headers={"accept": "application/json"}) as response:
self.assertEqual(response.headers["Content-Type"], "application/json")
self.assertEqual(response.status, 200)
content = response.json()
self.assertEqual(content["headers"]["Accept"], "application/json")
self.assertEqual(content["args"], {"foo":"bar"})
def test_post_form(self):
with post("http://httpbin.org/post", data={"foo": "bar"}).raise_for_status() as response:
content = response.json()
self.assertEqual(content["headers"]["Content-Type"], "application/x-www-form-urlencoded")
self.assertEqual(content["form"], {"foo":"bar"})
def test_post_string(self):
with post("http://httpbin.org/post", data="foobar").raise_for_status() as response:
content = response.json()
self.assertEqual(content["headers"]["Content-Type"], "text/plain; charset=UTF-8")
self.assertEqual(content["data"], "foobar")
def test_post_json(self):
with post("http://httpbin.org/post", json={"foo":"bar"}).raise_for_status() as response:
content = response.json()
self.assertEqual(content["headers"]["Content-Type"], "application/json")
self.assertEqual(content["json"], {"foo":"bar"})
2018-07-16 18:04:42 -04:00
if __name__ == '__main__':
unittest.main()