-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
124 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
# -*- coding: utf-8 -*- | ||
from .base import BaseOperator, Operator | ||
|
||
|
||
class MethodOperator(BaseOperator): | ||
""" | ||
Asserts HTTP request/response method. | ||
Example:: | ||
# Should style | ||
res | should.be.method('GET') | ||
# Should style - negation form | ||
res | should.not_be.method('GET') | ||
# Expect style | ||
res | expect.to.be.method('GET') | ||
# Expect style - negation form | ||
res | expect.to_not.be.method('GET') | ||
""" | ||
|
||
# Defines operator kind | ||
kind = Operator.Type.MATCHER | ||
|
||
# Operator keywords | ||
operators = ('method', 'verb') | ||
|
||
# Operator aliases | ||
aliases = ('name', 'equal', 'to', 'be', 'of') | ||
|
||
# Error message templates | ||
expected_message = Operator.Dsl.Message( | ||
'a request method that is equal to "{value}"', | ||
'a request method that is not equal to "{value}"', | ||
) | ||
|
||
# Subject message template | ||
subject_message = Operator.Dsl.Message( | ||
'a request method "{value}"', | ||
) | ||
|
||
def _match(self, res, method): | ||
# Define subject as request method | ||
self.subject = res.method | ||
|
||
if not isinstance(method, str): | ||
return False, ['method argument must be a string'] | ||
|
||
if not method: | ||
return False, ['method argument cannot be empty'] | ||
|
||
return method.upper() == res.method, ['request method does not match'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,16 @@ | ||
from .operators import get | ||
|
||
# Stores if the plugin has been registered | ||
registered = False | ||
|
||
|
||
def register(engine): | ||
""" | ||
Plugin register function. | ||
""" | ||
global registered | ||
if registered: | ||
return | ||
|
||
registered = True | ||
engine.register(*get()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import pook | ||
import pytest | ||
import requests | ||
|
||
|
||
def test_method_presence(should): | ||
pook.get('foo.com', reply=200, response_type='json') | ||
res = requests.get('http://foo.com') | ||
|
||
res | should.have.method('GET') | ||
res | should.have.method.equal.to('GET') | ||
res | should.have_not.method.equal.to('POST') | ||
|
||
with pytest.raises(AssertionError): | ||
res | should.have.method.equal.to('POST') | ||
|
||
with pytest.raises(AssertionError): | ||
res | should.have_not.method.equal.to('GET') |