|
| 1 | +import pytest |
| 2 | + |
| 3 | +from form2request import Request |
| 4 | + |
| 5 | +web_poet = pytest.importorskip("web_poet") |
| 6 | +scrapy = pytest.importorskip("scrapy") |
| 7 | +requests = pytest.importorskip("requests") |
| 8 | + |
| 9 | + |
| 10 | +def fake_scrapy_callback(self, response): |
| 11 | + pass |
| 12 | + |
| 13 | + |
| 14 | +@pytest.mark.parametrize( |
| 15 | + ("request_data", "method", "kwargs", "expected"), |
| 16 | + ( |
| 17 | + # GET |
| 18 | + *( |
| 19 | + ( |
| 20 | + Request( |
| 21 | + url="https://example.com?foo=bar", |
| 22 | + method="GET", |
| 23 | + headers=[], |
| 24 | + body=b"", |
| 25 | + ), |
| 26 | + method, |
| 27 | + kwargs, |
| 28 | + expected, |
| 29 | + ) |
| 30 | + for method, kwargs, expected in ( |
| 31 | + ( |
| 32 | + "poet", |
| 33 | + {}, |
| 34 | + web_poet.HttpRequest( |
| 35 | + url=web_poet.RequestUrl("https://example.com?foo=bar"), |
| 36 | + method="GET", |
| 37 | + headers=web_poet.HttpRequestHeaders(), |
| 38 | + body=web_poet.HttpRequestBody(b""), |
| 39 | + ), |
| 40 | + ), |
| 41 | + ( |
| 42 | + "requests", |
| 43 | + {}, |
| 44 | + requests.Request("GET", "https://example.com?foo=bar").prepare(), |
| 45 | + ), |
| 46 | + ( |
| 47 | + "scrapy", |
| 48 | + {"callback": fake_scrapy_callback}, |
| 49 | + scrapy.Request( |
| 50 | + "https://example.com?foo=bar", callback=fake_scrapy_callback |
| 51 | + ), |
| 52 | + ), |
| 53 | + ) |
| 54 | + ), |
| 55 | + # POST |
| 56 | + *( |
| 57 | + ( |
| 58 | + Request( |
| 59 | + url="https://example.com", |
| 60 | + method="POST", |
| 61 | + headers=[("Content-Type", "application/x-www-form-urlencoded")], |
| 62 | + body=b"foo=bar", |
| 63 | + ), |
| 64 | + method, |
| 65 | + kwargs, |
| 66 | + expected, |
| 67 | + ) |
| 68 | + for method, kwargs, expected in ( |
| 69 | + ( |
| 70 | + "poet", |
| 71 | + {}, |
| 72 | + web_poet.HttpRequest( |
| 73 | + url=web_poet.RequestUrl("https://example.com"), |
| 74 | + method="POST", |
| 75 | + headers=web_poet.HttpRequestHeaders( |
| 76 | + {"Content-Type": "application/x-www-form-urlencoded"} |
| 77 | + ), |
| 78 | + body=web_poet.HttpRequestBody(b"foo=bar"), |
| 79 | + ), |
| 80 | + ), |
| 81 | + ( |
| 82 | + "requests", |
| 83 | + {}, |
| 84 | + requests.Request( |
| 85 | + "POST", |
| 86 | + "https://example.com", |
| 87 | + headers={"Content-Type": "application/x-www-form-urlencoded"}, |
| 88 | + data=b"foo=bar", |
| 89 | + ).prepare(), |
| 90 | + ), |
| 91 | + ( |
| 92 | + "scrapy", |
| 93 | + {"callback": fake_scrapy_callback}, |
| 94 | + scrapy.Request( |
| 95 | + "https://example.com", |
| 96 | + method="POST", |
| 97 | + headers={"Content-Type": "application/x-www-form-urlencoded"}, |
| 98 | + body=b"foo=bar", |
| 99 | + callback=fake_scrapy_callback, |
| 100 | + ), |
| 101 | + ), |
| 102 | + ) |
| 103 | + ), |
| 104 | + # kwargs |
| 105 | + ( |
| 106 | + Request( |
| 107 | + url="https://example.com", |
| 108 | + method="POST", |
| 109 | + headers=[("Content-Type", "application/x-www-form-urlencoded")], |
| 110 | + body=b"foo=bar", |
| 111 | + ), |
| 112 | + "requests", |
| 113 | + {"params": {"foo": "bar"}}, |
| 114 | + requests.Request( |
| 115 | + "POST", |
| 116 | + "https://example.com?foo=bar", |
| 117 | + headers={"Content-Type": "application/x-www-form-urlencoded"}, |
| 118 | + data=b"foo=bar", |
| 119 | + ).prepare(), |
| 120 | + ), |
| 121 | + ( |
| 122 | + Request( |
| 123 | + url="https://example.com", |
| 124 | + method="POST", |
| 125 | + headers=[("Content-Type", "application/x-www-form-urlencoded")], |
| 126 | + body=b"foo=bar", |
| 127 | + ), |
| 128 | + "scrapy", |
| 129 | + {"callback": fake_scrapy_callback, "meta": {"foo": "bar"}}, |
| 130 | + scrapy.Request( |
| 131 | + "https://example.com", |
| 132 | + method="POST", |
| 133 | + headers={"Content-Type": "application/x-www-form-urlencoded"}, |
| 134 | + body=b"foo=bar", |
| 135 | + callback=fake_scrapy_callback, |
| 136 | + meta={"foo": "bar"}, |
| 137 | + ), |
| 138 | + ), |
| 139 | + ), |
| 140 | +) |
| 141 | +def test_conversion(request_data, method, kwargs, expected): |
| 142 | + actual = getattr(request_data, f"to_{method}")(**kwargs) |
| 143 | + if method == "poet": |
| 144 | + for field in ("method", "headers", "body"): |
| 145 | + assert getattr(actual, field) == getattr(expected, field) |
| 146 | + # RequestUrl(…) != RequestUrl(…) |
| 147 | + assert str(actual.url) == str(expected.url) |
| 148 | + elif method == "requests": |
| 149 | + # Request(…).prepare() != Request(…).prepare() |
| 150 | + for field in ("url", "method", "headers", "body"): |
| 151 | + assert getattr(actual, field) == getattr(expected, field) |
| 152 | + else: |
| 153 | + assert method == "scrapy" |
| 154 | + # Request(…) != Request(…) |
| 155 | + for field in ("url", "method", "headers", "body", "callback", "meta"): |
| 156 | + assert getattr(actual, field) == getattr(expected, field) |
0 commit comments