A simple mock of HttpRequest Pass in a dictionary to the constructor that maps request methodIds to tuples of (httplib2.Response, content, opt_expected_body) that should be returned when that method is called. None may also be passed in for the httplib2.Response, in which case a 200
| 1639 | |
| 1640 | |
| 1641 | class RequestMockBuilder(object): |
| 1642 | """A simple mock of HttpRequest |
| 1643 | |
| 1644 | Pass in a dictionary to the constructor that maps request methodIds to |
| 1645 | tuples of (httplib2.Response, content, opt_expected_body) that should be |
| 1646 | returned when that method is called. None may also be passed in for the |
| 1647 | httplib2.Response, in which case a 200 OK response will be generated. |
| 1648 | If an opt_expected_body (str or dict) is provided, it will be compared to |
| 1649 | the body and UnexpectedBodyError will be raised on inequality. |
| 1650 | |
| 1651 | Example: |
| 1652 | response = '{"data": {"id": "tag:google.c...' |
| 1653 | requestBuilder = RequestMockBuilder( |
| 1654 | { |
| 1655 | 'plus.activities.get': (None, response), |
| 1656 | } |
| 1657 | ) |
| 1658 | googleapiclient.discovery.build("plus", "v1", requestBuilder=requestBuilder) |
| 1659 | |
| 1660 | Methods that you do not supply a response for will return a |
| 1661 | 200 OK with an empty string as the response content or raise an excpetion |
| 1662 | if check_unexpected is set to True. The methodId is taken from the rpcName |
| 1663 | in the discovery document. |
| 1664 | |
| 1665 | For more details see the project wiki. |
| 1666 | """ |
| 1667 | |
| 1668 | def __init__(self, responses, check_unexpected=False): |
| 1669 | """Constructor for RequestMockBuilder |
| 1670 | |
| 1671 | The constructed object should be a callable object |
| 1672 | that can replace the class HttpResponse. |
| 1673 | |
| 1674 | responses - A dictionary that maps methodIds into tuples |
| 1675 | of (httplib2.Response, content). The methodId |
| 1676 | comes from the 'rpcName' field in the discovery |
| 1677 | document. |
| 1678 | check_unexpected - A boolean setting whether or not UnexpectedMethodError |
| 1679 | should be raised on unsupplied method. |
| 1680 | """ |
| 1681 | self.responses = responses |
| 1682 | self.check_unexpected = check_unexpected |
| 1683 | |
| 1684 | def __call__( |
| 1685 | self, |
| 1686 | http, |
| 1687 | postproc, |
| 1688 | uri, |
| 1689 | method="GET", |
| 1690 | body=None, |
| 1691 | headers=None, |
| 1692 | methodId=None, |
| 1693 | resumable=None, |
| 1694 | ): |
| 1695 | """Implements the callable interface that discovery.build() expects |
| 1696 | of requestBuilder, which is to build an object compatible with |
| 1697 | HttpRequest.execute(). See that method for the description of the |
| 1698 | parameters and the expected response. |
no outgoing calls
searching dependent graphs…