33import pytest
44
55from scrapingbee import ScrapingBeeClient
6- from scrapingbee .default_headers import default_headers
6+ from scrapingbee .utils import DEFAULT_HEADERS
77
88
99@pytest .fixture (scope = 'module' )
1010def client ():
1111 return ScrapingBeeClient (api_key = 'API_KEY' )
1212
1313
14- @mock .patch ('scrapingbee.client.request ' )
15- def test_get (mock_request , client ):
14+ @mock .patch ('scrapingbee.client.Session ' )
15+ def test_get (mock_session , client ):
1616 '''It should make a GET request with the url and API key'''
1717 client .get ('https://httpbin.org' )
1818
19- mock_request .assert_called_with (
19+ mock_session . return_value . request .assert_called_with (
2020 'GET' ,
2121 'https://app.scrapingbee.com/api/v1/'
2222 '?api_key=API_KEY&url=https%3A//httpbin.org' ,
2323 data = None ,
24- headers = default_headers
24+ headers = DEFAULT_HEADERS
2525 )
2626
2727
28- @mock .patch ('scrapingbee.client.request ' )
29- def test_get_with_params (mock_request , client ):
28+ @mock .patch ('scrapingbee.client.Session ' )
29+ def test_get_with_params (mock_session , client ):
3030 '''It should add parameters to the url'''
3131 client .get ('https://httpbin.org' , params = {'render_js' : True })
3232
33- mock_request .assert_called_with (
33+ mock_session . return_value . request .assert_called_with (
3434 'GET' ,
3535 'https://app.scrapingbee.com/api/v1/'
3636 '?api_key=API_KEY&url=https%3A//httpbin.org&render_js=True' ,
3737 data = None ,
38- headers = default_headers ,
38+ headers = DEFAULT_HEADERS ,
3939 )
4040
4141
42- @mock .patch ('scrapingbee.client.request ' )
43- def test_get_with_headers (mock_request , client ):
42+ @mock .patch ('scrapingbee.client.Session ' )
43+ def test_get_with_headers (mock_session , client ):
4444 '''It should prefix header names with Spb- and set forward_headers'''
4545 client .get ('https://httpbin.org' , headers = {'Content-Type' : 'text/html; charset=utf-8' })
4646
47- mock_request .assert_called_with (
47+ mock_session . return_value . request .assert_called_with (
4848 'GET' ,
4949 'https://app.scrapingbee.com/api/v1/'
5050 '?api_key=API_KEY&url=https%3A//httpbin.org&forward_headers=True' ,
5151 data = None ,
5252 headers = {'Spb-Content-Type' : 'text/html; charset=utf-8' ,
53- ** default_headers },
53+ ** DEFAULT_HEADERS },
5454 )
5555
5656
57- @mock .patch ('scrapingbee.client.request ' )
58- def test_get_with_cookies (mock_request , client ):
57+ @mock .patch ('scrapingbee.client.Session ' )
58+ def test_get_with_cookies (mock_session , client ):
5959 '''It should format the cookies and add them to the url'''
6060 client .get ('https://httpbin.org' , cookies = {
6161 'name_1' : 'value_1' ,
6262 'name_2' : 'value_2' ,
6363 })
6464
65- mock_request .assert_called_with (
65+ mock_session . return_value . request .assert_called_with (
6666 'GET' ,
6767 'https://app.scrapingbee.com/api/v1/'
6868 '?api_key=API_KEY&url=https%3A//httpbin.org&cookies=name_1=value_1;name_2=value_2' ,
6969 data = None ,
70- headers = default_headers ,
70+ headers = DEFAULT_HEADERS ,
7171 )
7272
7373
74- @mock .patch ('scrapingbee.client.request ' )
75- def test_get_with_extract_rules (mock_request , client ):
74+ @mock .patch ('scrapingbee.client.Session ' )
75+ def test_get_with_extract_rules (mock_session , client ):
7676 '''It should format the extract_rules and add them to the url'''
7777 client .get ('https://httpbin.org' , params = {
7878 'extract_rules' : {
@@ -81,19 +81,19 @@ def test_get_with_extract_rules(mock_request, client):
8181 }
8282 })
8383
84- mock_request .assert_called_with (
84+ mock_session . return_value . request .assert_called_with (
8585 'GET' ,
8686 'https://app.scrapingbee.com/api/v1/'
8787 '?api_key=API_KEY&url=https%3A//httpbin.org&'
8888 'extract_rules=%7B%22title%22%3A%20%22h1%22%2C%20%22'
8989 'subtitle%22%3A%20%22%23subtitle%22%7D' ,
9090 data = None ,
91- headers = default_headers ,
91+ headers = DEFAULT_HEADERS ,
9292 )
9393
9494
95- @mock .patch ('scrapingbee.client.request ' )
96- def test_get_with_js_scenario (mock_request , client ):
95+ @mock .patch ('scrapingbee.client.Session ' )
96+ def test_get_with_js_scenario (mock_session , client ):
9797 '''It should format the extract_rules and add them to the url'''
9898 client .get ('https://httpbin.org' , params = {
9999 'js_scenario' : {
@@ -103,24 +103,24 @@ def test_get_with_js_scenario(mock_request, client):
103103 }
104104 })
105105
106- mock_request .assert_called_with (
106+ mock_session . return_value . request .assert_called_with (
107107 'GET' ,
108108 'https://app.scrapingbee.com/api/v1/'
109109 '?api_key=API_KEY&url=https%3A//httpbin.org&'
110110 'js_scenario=%7B%22instructions%22%3A%20%5B%7B%22click%22%3A%20%22%23buttonId%22%7D%5D%7D' ,
111111 data = None ,
112- headers = default_headers ,
112+ headers = DEFAULT_HEADERS ,
113113 )
114114
115115
116- @mock .patch ('scrapingbee.client.request ' )
117- def test_post (mock_request , client ):
116+ @mock .patch ('scrapingbee.client.Session ' )
117+ def test_post (mock_session , client ):
118118 '''It should make a POST request with some data'''
119119 client .post ('https://httpbin.org' , data = {'KEY_1' : 'VALUE_1' })
120120
121- mock_request .assert_called_with (
121+ mock_session . return_value . request .assert_called_with (
122122 'POST' ,
123123 'https://app.scrapingbee.com/api/v1/?api_key=API_KEY&url=https%3A//httpbin.org' ,
124124 data = {'KEY_1' : 'VALUE_1' },
125- headers = default_headers
125+ headers = DEFAULT_HEADERS
126126 )
0 commit comments