Opened 12 years ago

Last modified 11 years ago

#17631 new Cleanup/optimization

edge case: django.test.client should handle fields and files with the same name

Reported by: Damien Nozay Owned by:
Component: Testing framework Version: 1.3
Severity: Normal Keywords:
Cc: oliver@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

problem:

  • want to upload file with very common name, e.g. 'somefield'
  • want to post data with very common name for field, e.g. 'somefield'

http://www.scotthawker.com/scott/?p=1892

http://code.activestate.com/recipes/146306/

>>> fields = { 'somefield': 1 }
>>> files = { 'somefield': open('/dev/null', 'r') }
>>> data = QueryDict('', mutable=True)
>>> data.update(fields)
>>> data.update(files)
>>> data
<QueryDict: {u'somefield': [1, <open file '/dev/null', mode 'r' at 0x8a0c610>]}>
>>> data.items()
[(u'somefield', <open file '/dev/null', mode 'r' at 0x8a0c610>)]
>>> data
<QueryDict: {u'somefield': [1, <open file '/dev/null', mode 'r' at 0x8a0c610>]}>
>>> 
>>> from django.utils.datastructures import MultiValueDict
>>> data = MultiValueDict()
>>> data.update(fields)
>>> data.update(files)
>>> data
<MultiValueDict: {'somefield': [1, <open file '/dev/null', mode 'r' at 0x8a0c610>]}>
>>> data.items()
[('somefield', <open file '/dev/null', mode 'r' at 0x8a0c610>)]
>>> data['somefield']
<open file '/dev/null', mode 'r' at 0x8a0c610>
>>> data.getlist('somefield')
[1, <open file '/dev/null', mode 'r' at 0x8a0c610>]
>>> 

https://code.djangoproject.com/browser/django/trunk/django/test/client.py

116	    # Each bit of the multipart form data could be either a form value or a
117	    # file, or a *list* of form values and/or files. Remember that HTTP field
118	    # names can be duplicated!
119	    for (key, value) in data.items():

IMHO, the test client should use data.lists() instead when providing MultiValueDict or subclass.

Change History (5)

comment:1 by Łukasz Rekucki, 12 years ago

Owner: changed from nobody to Łukasz Rekucki
Triage Stage: UnreviewedAccepted

comment:2 by Łukasz Rekucki, 12 years ago

Owner: Łukasz Rekucki removed

comment:3 by Oliver Beattie, 12 years ago

Cc: oliver@… added

comment:4 by Aymeric Augustin, 11 years ago

Component: UncategorizedTesting framework

comment:5 by Aymeric Augustin, 11 years ago

Type: UncategorizedCleanup/optimization
Note: See TracTickets for help on using tickets.
Back to Top