File: test_graph_redirect.py

package info (click to toggle)
rdflib 7.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 77,852 kB
  • sloc: python: 59,555; sh: 153; makefile: 83; ruby: 74; xml: 45
file content (47 lines) | stat: -rw-r--r-- 1,426 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
from __future__ import annotations

from typing import Tuple
from urllib.parse import urlparse

from rdflib.graph import Graph
from test.data import SIMPLE_TRIPLE_GRAPH, TEST_DATA_DIR
from test.utils import GraphHelper
from test.utils.http import MethodName, MockHTTPResponse
from test.utils.httpservermock import ServedBaseHTTPServerMock


def test_graph_redirect_new_host(
    function_httpmocks: Tuple[ServedBaseHTTPServerMock, ServedBaseHTTPServerMock]
) -> None:
    """
    Redirect to new host results in a request with the right Host header
    parameter.
    """

    mock_a, mock_b = function_httpmocks

    mock_a.responses[MethodName.GET].append(
        MockHTTPResponse(
            308,
            "Permanent Redirect",
            b"",
            {"Location": [f"{mock_b.url}/b/data.ttl"]},
        )
    )

    mock_b.responses[MethodName.GET].append(
        MockHTTPResponse(
            200,
            "OK",
            (TEST_DATA_DIR / "variants" / "simple_triple.ttl").read_bytes(),
            {"Content-Type": ["text/turtle"]},
        )
    )

    graph = Graph()
    graph.parse(location=f"{mock_a.url}/a/data.ttl")
    GraphHelper.assert_sets_equals(graph, SIMPLE_TRIPLE_GRAPH)
    for mock in function_httpmocks:
        assert 1 == len(mock.requests[MethodName.GET])
        for request in mock.requests[MethodName.GET]:
            assert request.headers["Host"] == urlparse(mock.url).netloc