Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove mocket #173

Merged
merged 6 commits into from
Jul 16, 2024
Merged

Remove mocket #173

merged 6 commits into from
Jul 16, 2024

Conversation

shadromani
Copy link
Contributor

No description provided.

@shadromani shadromani force-pushed the sromani/remove-mocket branch from 47b008d to 55f57b3 Compare July 4, 2024 20:31
@shadromani shadromani force-pushed the sromani/remove-mocket branch from 55f57b3 to 8a58936 Compare July 4, 2024 20:49
Copy link
Member

@oschwald oschwald left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! I had a few comments.

httpretty.GET,
self.base_uri + "country/1.2.3.4",
body=json.dumps(self.country),
self.httpserver.expect_request("/geoip/v2.1/country/1.2.3.4").respond_with_json(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should specify the method on all of the expect_request calls as the default is to allow all methods.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will you please elaborate how? The test has different responses depending on the specific ip and we can not return the default on all requests.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if I completely understand the question, but I am referring to the HTTP method. If we don't set it explicitly to GET, any method will be allowed. To do so, I think you would add something like this to each of them:

Suggested change
self.httpserver.expect_request("/geoip/v2.1/country/1.2.3.4").respond_with_json(
self.httpserver.expect_request("/geoip/v2.1/country/1.2.3.4", method="GET").respond_with_json(

This would be equivalent to httpretty.GET in the previous version.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I misread your comment.

tests/webservice_test.py Outdated Show resolved Hide resolved
self.run_client(self.client.country("1.2.3.4"))
except Exception as e:
# just to avoid the exception
pass
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to return a valid JSON response. I think if you use the header matchers, this will be easy to do with respond_with_json.

def custom_handler(r):
nonlocal request
request = r
return ""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The custom handler is a bit confusing. I think we could eliminate it by using headers or header_value_matcher to match the expected headers on expect_request. I don't think we need to test the path again.

tests/webservice_test.py Outdated Show resolved Hide resolved
tests/webservice_test.py Show resolved Hide resolved
tests/webservice_test.py Outdated Show resolved Hide resolved
@shadromani shadromani force-pushed the sromani/remove-mocket branch from 0d0c317 to 94c3584 Compare July 8, 2024 21:11
matcher = HeaderValueMatcher({
"Accept": "application/json",
"Authorization":"Basic NDI6YWJjZGVmMTIzNDU2",
"User-Agent": lambda x: x.startswith("GeoIP2-Python-Client/"),})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the correct usage of HeaderValueMatcher. I think you still set headers, but you would want to define a custom matcher for "User-Agent".

See the docs.

"Accept": accept_compare,
"Authorization": authorization_compare,
}
),
Copy link
Member

@oschwald oschwald Jul 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this works either. If I change the values, the tests don't fail. I was expecting something like this:

        def user_agent_compare(actual: str, expected: str) -> bool:
            if actual is None:
                return False
            return actual.startswith(expected)

        self.httpserver.expect_request(
            "/geoip/v2.1/country/1.2.3.4",
            method="GET",
            headers={
                    "Accept": "application/json",
                    "Authorization": "Basic NDI6YWJjZGVmMTIzNDU2",
                    "User-Agent": "GeoIP2-Python-Client/"
            },
            header_value_matcher=HeaderValueMatcher(defaultdict(
                lambda: HeaderValueMatcher.default_header_value_matcher,
                {"User-Agent": user_agent_compare}
            )),
        ).respond_with_json(
            self.country,
            status=200,
            content_type=self._content_type("country"),
        )

Alternatively, you could modify the default matcher as directed in the docs.

@oschwald oschwald merged commit 910f320 into main Jul 16, 2024
25 checks passed
@oschwald oschwald deleted the sromani/remove-mocket branch July 16, 2024 14:38
@deronnax
Copy link
Contributor

I'm curious, what's the rational behind the switch from mocket to http-server?

@oschwald
Copy link
Member

We unfortunately had quite a few problem with it breaking, e.g., on new or old Python versions, on new aiohttp versions, on particular OSes, etc. If you search the pull requests for "mocket", you will see a subset of those.

We decided to use pytest-httpserver as it implements an actual HTTP server, which should be less prone to these sorts of issues.

@akx akx mentioned this pull request Aug 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants