title: Flask testing, post file and nested dictionaries
body: <p>I have created a Flask app and wanted to test it. In a single endpoint, I would like to post a multipart request, which includes a file and a complex JSON object. I thought at first of using werkzeug EnvironBuilder for this task, as it seems to provide a quite automated approach, handling content types, etc. My snippet of code for preparing the request is the following:</p>
  
  <pre><code># client is an instance of FlaskClient produced using a pytest fixture and the method test client
  
  def _post(endpoint, file_path=None, serialized_message=None):
          with open(file_path, 'rb') as fin:
                  fil = io.BytesIO(fin.read())
          file_name = file_path.split(os.sep)[-1]
          builder = EnvironBuilder(path='/' + endpoint,
                                   method='POST',
                                    data=json.loads(
                                        serialized_message),
                                  content_type="application/json")
          builder.files[file_name] = fil
          result = client.open(builder, buffered=True)
          return result
  </code></pre>
  
  <p>This failed with the following error:</p>
  
  <pre><code> def _add_file_from_data(self, key, value):
          """Called in the EnvironBuilder to add files from the data dict."""
          if isinstance(value, tuple):
              self.files.add_file(key, *value)
          elif isinstance(value, dict):
              from warnings import warn
              warn(DeprecationWarning('it\'s no longer possible to pass dicts '
                                      'as `data`.  Use tuples or FileStorage '
                                      'objects instead'), stacklevel=2)
              value = dict(value)
              mimetype = value.pop('mimetype', None)
              if mimetype is not None:
                  value['content_type'] = mimetype
  &gt;           self.files.add_file(key, **value)
  E           TypeError: add_file() got an unexpected keyword argument 'globalServiceOptionId'
  </code></pre>
  
  <p>With the <code>globalServiceOptionId</code> being a key of a nested dictionary in the dictionary I am posting. I have some thoughts over bypassing this problem, with converting to string jsons the inner dictionaries, but I would like something more concrete as an answer, as I do not want the representation of the request to be changed inside and outside of testing. Thank you.</p>
  
  <h2>Update 1</h2>
  
  <p>The form of the passwed dictionary doesn't really matter, as long as it has nested dictionaries inside it. This json is given in this example:</p>
  
  <pre><code>{
      "attachments": [],
      "Ids": [],
      "globalServiceOptions": [{
        "globalServiceOptionId": {
          "id": 2,
          "agentServiceId": {
            "id": 2
          },
          "serviceOptionName": "Time",
          "value": "T_last",
          "required": false,
          "defaultValue": "T_last",
          "description": "UTC Timestamp",
          "serviceOptionType": "TIME"
        },
        "name": "Time",
        "value": null
      }]
    }
  </code></pre>
  
  <h2>Update 2</h2>
  
  <p>I tested another snippet:</p>
  
  <pre><code>    def _post(endpoint, file_path=None, serialized_message=None):
          with open(file_path, 'rb') as fin:
                  fil = io.BytesIO(fin.read())
          files = {
           'file': (file_path, fil, 'application/octet-stream')
          }
          for key, item in json.loads(serialized_message).items():
              files[key] = (None, json.dumps(item), 'application/json')
  
          builder = EnvironBuilder(path='/' + endpoint,
                                   method='POST', data=files,
                                   )
          result = client.open(builder, buffered=True)
          return result
  </code></pre>
  
  <p>Although this runs without errors, Flask recognizes (as expected) the incoming jsons as files, which again requires different handling during testing and normal running.</p>