
                         ^   S r SSKJr  SSKrSSKrSSKJr  SSKJr  SSKJ	r	  \	R                  (       a  SSKr\R                  R                  rOSSKr\R                  r " S S\5      r " S	 S
\\5      r " S S\\5      r " S S\5      r " S S\5      r " S S\\R,                  5      rg)z@Utilities for testing code that uses appengine_rpc's *RpcServer.    )absolute_importN)AbstractRpcServer)HttpRpcServer)
six_subsetc                   ^    \ rS rSrSrSS jrSS jrS r " S S\5      r	 " S S	\5      r
S
rg)TestRpcServerMixin)   zDProvides a mocked-out version of HttpRpcServer for testing purposes.c                 :    U R                   R                  U5        gzEnables strict mode.N)opener
set_strictselfstricts     =lib/googlecloudsdk/appengine/tools/appengine_rpc_test_util.pyr   TestRpcServerMixin.set_strict,   s    KK6"    c                 :    U R                   R                  U5        gz.Enables saving request data for every request.N)r   set_save_request_datar   save_request_datas     r   r   (TestRpcServerMixin.set_save_request_data0   s    KK%%&78r   c                 *    [         R                  5       $ )z7Returns a MockOpener.

Returns:
  A MockOpener object.
)r   
MockOpenerr   s    r   
_GetOpenerTestRpcServerMixin._GetOpener4   s     ((**r   c                   >    \ rS rSrSrS
S jrS rSS jrS rS r	S	r
g)TestRpcServerMixin.MockResponse<   z2A mocked out response object for testing purposes.Nc                     [         R                  " U5      U l        X l        X0l        SU l        U R                  c  0 U l        gg)zCreates a new MockResponse.

Args:
  body: The text of the body to return.
  code: The response code (default 200).
  headers: An optional header dictionary.
 N)ioBytesIOfpcodeheadersmsg)r   bodyr'   r(   s       r   __init__(TestRpcServerMixin.MockResponse.__init__?   s;     

4 dgildh		 
r   c                     U R                   $ Nr(   r   s    r   info$TestRpcServerMixin.MockResponse.infoO   s    \\r   c                 8    U R                   R                  U5      $ )zrReads from the response body.

Args:
  length: The number of bytes to read.

Returns:
  The body of the response.
)r&   read)r   lengths     r   r3   $TestRpcServerMixin.MockResponse.readR   s     WW\\&!!r   c                 6    U R                   R                  5       $ )zXReads a line from the response body.

Returns:
  A line of text from the response body.
)r&   readliner   s    r   r7   (TestRpcServerMixin.MockResponse.readline]   s     WWr   c                 8    U R                   R                  5         g)zCloses the response stream.N)r&   closer   s    r   r:   %TestRpcServerMixin.MockResponse.closee   s    
ggmmor   )r'   r&   r(   r)   )   N))__name__
__module____qualname____firstlineno____doc__r+   r0   r3   r7   r:   __static_attributes__ r   r   MockResponser    <   s    < 	" r   rE   c                   J    \ rS rSrSrS rSS jrSS jrS rS r	S r
S	 rS
rg)TestRpcServerMixin.MockOpeneri   z1A mocked-out OpenerDirector for testing purposes.c                 f    / U l         / U l        0 U l        0 U l        SU l        SU l        SU l        g)zCreates a new MockOpener.NF)request_datarequests	responsesordered_responsescookier   r   r   s    r   r+   &TestRpcServerMixin.MockOpener.__init__l   s6    ddmdn!ddkdk$dr   c                     Xl         gr   )r   r   s     r   r   (TestRpcServerMixin.MockOpener.set_strictv   s    kr   c                     Xl         gr   )r   r   s     r   r   3TestRpcServerMixin.MockOpener.set_save_request_dataz   s    0r   c                 p   UR                  5       nSU;   a  USUR                  S5       nOUnUS:w  a:  UR                  S5      (       d$  SUR                  ;   d   eSUR                  ;   d   eU[	        UR
                  5      4nU R                  R                  U5        U R                  (       a&  U R                  R                  X!R
                  45        U R                  (       a.  U R                  UR                  S'   U R                  U   " U5      nX0R                  ;   am  [        R                  " SU-  5        U R                  U   R                  S	5      " U5      nU R                  U   (       d  U R                  R                  U5        OX0R                  ;   a/  [        R                  " S
U-  5        U R                  U   " U5      nOSU R                   (       a  [#        SU< SU< S35      e[        R                  " SU-  5        [$        R'                  S5      nSUR                  ;   a  UR                  S   U l
        SUR(                  s=::  a  S:  dX  O  UR(                  UR*                  UR-                  5       pn[.        R0                  " UR3                  5       5      n	[5        X6SXS9eU$ )z3Logs the request and returns a MockResponse object.?Nz+https://www.google.com/accounts/ClientLoginz	_ah/loginzX-appcfg-api-versionz
User-agentCookiez)Using ordered pre-canned response for: %sr   z!Using pre-canned response for: %szNo response found for url: z ()z$Using generic blank response for: %sr   z
Set-Cookier<   i,  )urlr'   r)   hdrsr&   )get_full_urlfindendswithr(   booldatarK   appendr   rJ   rN   rL   rM   loggingdebugpopr   	Exceptionr   rE   r'   r)   r0   r$   r%   r3   	HTTPError)
r   requestfull_urlrX   rJ   responser'   r)   rY   r&   s
             r   open"TestRpcServerMixin.MockOpener.open~   s"   %%'h	*c*+
>
>,,{++%888w...W\\ 23l
mm<(			  (LL!9:	$(KK!>>#&w/ 
&&	&AHLM))#.2215g>%%c*

 
 
$
$S
).. 9HDE>>#&w/;;XNOO<xGH%2237	))	)&&|4 X]](S("--x}}4ZZ(C4GGor   c                      X R                   U'   g)zCalls the provided function when the provided URL is requested.

The provided function should accept a request object and return a
response object.

Args:
  url: The URL to trigger on.
  response_func: The function to call when the url is requested.
N)rL   r   rX   response_funcs      r   AddResponse)TestRpcServerMixin.MockOpener.AddResponse   s     *nnSr   c                 |    XR                   ;  a  / U R                   U'   U R                   U   R                  U5        g)aH  Calls the provided function when the provided URL is requested.

The provided functions should accept a request object and return a
response object.  This response will be added after previously given
responses if they exist.

Args:
  url: The URL to trigger on.
  response_func: The function to call when the url is requested.
N)rM   r_   rk   s      r   AddOrderedResponse0TestRpcServerMixin.MockOpener.AddOrderedResponse   s9     
**	*&(s#
S!((7r   c                      X R                   U'   g)a  Calls the provided function when the provided URL is requested.

The provided functions should accept a request object and return a
response object. Each response will be called once.

Args:
  url: The URL to trigger on.
  response_funcs: A list of response functions.
N)rM   )r   rX   response_funcss      r   AddOrderedResponses1TestRpcServerMixin.MockOpener.AddOrderedResponses   s     %3S!r   )rN   rM   rJ   rK   rL   r   r   NT)r>   r?   r@   rA   rB   r+   r   r   rh   rm   rp   rt   rC   rD   r   r   r   rG   i   s)    ;%1+Z
*8
3r   r   rD   Nrv   )r>   r?   r@   rA   rB   r   r   r   objectrE   r   rC   rD   r   r   r   r   )   s/    L#9++V +Zg36 g3r   r   c                       \ rS rSrSrg)TestRpcServer   rD   Nr>   r?   r@   rA   rC   rD   r   r   ry   ry          r   ry   c                       \ rS rSrSrg)TestHttpRpcServer   rD   Nr{   rD   r   r   r~   r~      r|   r   r~   c                   $    \ rS rSrSS jrS rSrg)UrlLibRequestResponseStub   Nc                 .    0 U l         U(       a  Xl         g g r.   r/   )r   r(   s     r   r+   "UrlLibRequestResponseStub.__init__   s    DLl r   c                      X R                   U'   g r.   r/   )r   headervalues      r   
add_header$UrlLibRequestResponseStub.add_header   s     !LLr   r/   r.   )r>   r?   r@   rA   r+   r   rC   rD   r   r   r   r      s    
!r   r   c                       \ rS rSrSrg)UrlLibRequestStub   rD   Nr{   rD   r   r   r   r      r|   r   r   c                       \ rS rSrS rSrg)UrlLibResponseStub   c                     [         R                  X5        U(       a   [        R                  R                  X5        O [        R                  R                  U S5        X0l        X@l        XPl        g )Nr   )r   r+   r$   r%   rX   r'   r)   )r   r*   r(   rX   r'   r)   s         r   r+   UrlLibResponseStub.__init__   sH    &&t5jj$%jj$$HIHr   )r'   r)   rX   N)r>   r?   r@   rA   r+   rC   rD   r   r   r   r      s    r   r   )rB   
__future__r   r$   r`   ,googlecloudsdk.appengine.tools.appengine_rpcr   r   "googlecloudsdk.appengine._internalr   PY3urllib.errorurlliberrorrd   urllib2rw   r   ry   r~   r   r   r%   r   rD   r   r   <module>r      s    G & 
  J F 9 >>ll$$))
g3 g3T&(9 *M 	! 	!1 	2BJJ 	r   