| | 394 | |
|---|
| | 395 | ###################################### |
|---|
| | 396 | # HttpResponse with Unicode headers # |
|---|
| | 397 | ###################################### |
|---|
| | 398 | |
|---|
| | 399 | >>> r = HttpResponse() |
|---|
| | 400 | |
|---|
| | 401 | If we insert a unicode value it will be converted to an ascii |
|---|
| | 402 | string. This makes sure we comply with the HTTP specifications. |
|---|
| | 403 | |
|---|
| | 404 | >>> r['value'] = u'test value' |
|---|
| | 405 | >>> isinstance(r['value'], str) |
|---|
| | 406 | True |
|---|
| | 407 | |
|---|
| | 408 | An error is raised When a unicode object with non-ascii is assigned. |
|---|
| | 409 | |
|---|
| | 410 | >>> r['value'] = u't\xebst value' # doctest:+ELLIPSIS |
|---|
| | 411 | Traceback (most recent call last): |
|---|
| | 412 | ... |
|---|
| | 413 | UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format |
|---|
| | 414 | |
|---|
| | 415 | The response also converts unicode keys to strings. |
|---|
| | 416 | |
|---|
| | 417 | >>> r[u'test'] = 'testing key' |
|---|
| | 418 | >>> list(sorted(r.items()))[1] |
|---|
| | 419 | ('test', 'testing key') |
|---|
| | 420 | |
|---|
| | 421 | It will also raise errors for keys with non-ascii data. |
|---|
| | 422 | |
|---|
| | 423 | >>> r[u't\xebst'] = 'testing key' # doctest:+ELLIPSIS |
|---|
| | 424 | Traceback (most recent call last): |
|---|
| | 425 | ... |
|---|
| | 426 | UnicodeEncodeError: ..., HTTP response headers must be in US-ASCII format |
|---|
| | 427 | |
|---|