title: Mocking external API for testing with Python
body: <p><strong>Context</strong></p>
  
  <p>I am trying to write tests for functions that query an external API. These functions send requests to the API, get responses and process them.
  In my tests, I want to simulate the external API with a Mock server that is ran locally.
  So far, the mock server is ran successfully and responds to custom GET queries.</p>
  
  <p><strong>The problem</strong></p>
  
  <p>The external API responds with objects of type <code>&lt;class 'dict'&gt;</code>, while apparently all I can get from my mock server is a response of type <code>&lt;class 'bytes'&gt;</code>. The mock server is fetching pre-defined data from disk and returning them through a stream. Since I can not simulate the external API, my tests throw error messages because of the wrong types of responses.</p>
  
  <p>Following are snippets of my code with some explanations.</p>
  
  <p><strong>1.</strong> <strong>setUp() function:</strong></p>
  
  <p>The setUp function is ran at the beginning of the test suite. It is responsible of configuring and running the server before running the tests:</p>
  
  <pre><code>def setUp(self):
      self.factory = APIRequestFactory()
      # Configuring the mock server
      self.mock_server_port = get_free_port()
      self.mock_server = HTTPServer(('localhost', self.mock_server_port), MockServerRequestHandler)
      # Run the mock server in a separate thread
      self.mock_server_thread = Thread(target=self.mock_server.serve_forever)
      self.mock_server_thread.setDaemon(True)
      self.mock_server_thread.start()
  </code></pre>
  
  <p><strong>2. The MockServerClassHandler:</strong></p>
  
  <pre><code>class MockServerRequestHandler(BaseHTTPRequestHandler):
  def do_GET(self):
      if re.search(config.SYSTEM_STATUS_PATTERN, self.path):
          # Response status code
          self.send_response(requests.codes.ok)
          # Response headers
          self.send_header("Content-Type", "application/json; charset=utf-8")
          self.end_headers()
          # Purge response from a file and serve it
          with open('/path/to/my-json-formatted-file') as data_file:
              response_content = json.dumps(json.load(data_file))
  
          # Writing to a stream that need bytes-like input
          # https://docs.python.org/2/library/basehttpserver.html
          self.wfile.write(response_content.encode('utf-8'))
          return
  </code></pre>
  
  <p>To my understanding from the <a href="https://docs.python.org/2/library/basehttpserver.html" rel="nofollow noreferrer">official documentation</a>, a <em>BaseHTTPRequestHandler</em> can only serve the content of his response through writing in a predefined stream (<code>wfile</code>) which needs to be given (and I am quoting an error message) a byte-like variable.</p>
  
  <p>So my questions are:</p>
  
  <ul>
  <li>Is there a way to make my mock server respond with other types of content than bytes? (JSON, python dicts ...)</li>
  <li>Is it safe to write, in the functions that I test, a piece of code that will convert the bytes variables to Python dicts, just so I can test them with my mock server? Or is this violating some principles of testing?</li>
  <li>Is there another way of writing a server that responds with JSON and python dicts?</li>
  </ul>