| | 394 | ###################################### |
| | 395 | # HttpResponse with Unicode headers # |
| | 396 | ###################################### |
| | 397 | |
| | 398 | >>> r = HttpResponse() |
| | 399 | |
| | 400 | If we insert a unicode value it will be converted to a string. This is done |
| | 401 | because the handlers (WSGI/mod_python) expect binary data and will break on |
| | 402 | non-ascii unicode. |
| | 403 | |
| | 404 | >>> r['value'] = u't\xebst value' |
| | 405 | >>> type(r['value']) |
| | 406 | <type 'str'> |
| | 407 | |
| | 408 | It uses the charset of the response for encoding it. By default it is utf-8. |
| | 409 | |
| | 410 | >>> type(r['value'].decode('utf8')) |
| | 411 | <type 'unicode'> |
| | 412 | |
| | 413 | The response also converts unicode keys to strings. |
| | 414 | |
| | 415 | >>> r[u't\xebst'] = 'testing key' |
| | 416 | >>> type(list(sorted(r.items()))[1][0]) |
| | 417 | <type 'str'> |
| | 418 | |
| | 419 | This only happens when you call items since the implementation uses a system |
| | 420 | where the original key is used for normal dict lookups. |
| | 421 | |
| | 422 | >>> u't\xebst' in r |
| | 423 | True |
| | 424 | |