title: Unit testing Python Google Cloud HTTP functions - need to pass in a request object
body: <p>I'm getting started writing Google Cloud http Functions using Python and I would like to include unit tests. My function only responds to POST requests so the basic outline of my function is:</p>
  
  <pre><code>def entry_point(request):
      if request.method == 'POST':
          # do something
      else:
          # throw error or similar
  </code></pre>
  
  <p>I want to write a simple unit test to ensure that if the function receives a GET request the response has a 405 status.</p>
  
  <p>Hence in my unit test I need to pass in a value for the <code>request</code> parameter:</p>
  
  <pre><code>def test_call(self):
      req = #need a way of constructing a request
  
      response = entry_point(req)
      assert response.status  == 405 # or something like this
  </code></pre>
  
  <p>Basically I need to construct a request then check that the response status is what I expect it to be. I've googled around and found loads of pages that talk about mocking and all sorts of stuff that I frankly don't understand (I'm not a crash hot developer) so I'm hoping someone can help me with an idiot's guide of doing what I need to do.</p>
  
  <p>I did find this: <a href="https://cloud.google.com/functions/docs/bestpractices/testing#unit_tests_2" rel="nofollow noreferrer">https://cloud.google.com/functions/docs/bestpractices/testing#unit_tests_2</a>: </p>
  
  <pre><code>from unittest.mock import Mock
  
  import main
  
  
  def test_print_name():
      name = 'test'
      data = {'name': name}
      req = Mock(get_json=Mock(return_value=data), args=data)
  
      # Call tested function
      assert main.hello_http(req) == 'Hello {}!'.format(name)
  
  
  def test_print_hello_world():
      data = {}
      req = Mock(get_json=Mock(return_value=data), args=data)
  
      # Call tested function
      assert main.hello_http(req) == 'Hello World!'
  </code></pre>
  
  <p>which <em>kinda</em> helped but it doesn't explain how I can specify the request method (i.e. GET, POST etc...).</p>