File tree Expand file tree Collapse file tree 5 files changed +73
-7
lines changed Expand file tree Collapse file tree 5 files changed +73
-7
lines changed Original file line number Diff line number Diff line change 11import { Component } from "./component.js" ;
2- import { isEmpty } from "./utils.js" ;
2+ import { isEmpty , hasValue } from "./utils.js" ;
33import { components } from "./store.js" ;
44
55let messageUrl = "" ;
6- const csrfTokenHeaderName = "X-CSRFToken" ;
6+ let csrfTokenHeaderName = "X-CSRFToken" ;
77
88/**
99 * Initializes the Unicorn object.
1010 */
11- export function init ( _messageUrl ) {
11+ export function init ( _messageUrl , _csrfTokenHeaderName ) {
1212 messageUrl = _messageUrl ;
13+
14+ if ( hasValue ( _csrfTokenHeaderName ) ) {
15+ csrfTokenHeaderName = _csrfTokenHeaderName ;
16+ }
17+
18+ return {
19+ messageUrl,
20+ csrfTokenHeaderName,
21+ } ;
1322}
1423
1524/**
Original file line number Diff line number Diff line change 55
66< script >
77 var url = "{% url 'django_unicorn:message' %}" ;
8- Unicorn . init ( url ) ;
8+ Unicorn . init ( url , "{{ CSRF_HEADER_NAME }}" ) ;
99</ script >
1010{% else %}
1111< script type ="module ">
1515 window . Unicorn = Unicorn ;
1616
1717 var url = "{% url 'django_unicorn:message' %}" ;
18- Unicorn . init ( url ) ;
18+ Unicorn . init ( url , "{{ CSRF_HEADER_NAME }}" ) ;
1919</ script >
2020{% endif %}
Original file line number Diff line number Diff line change 1313
1414@register .inclusion_tag ("unicorn/scripts.html" )
1515def unicorn_scripts ():
16- # Import here to prevent th potential of this from loading before Django settings
16+ # Import here to prevent the potential of this from loading before Django settings
1717 from django_unicorn .settings import get_setting
1818
19- return {"MINIFIED" : get_setting ("MINIFIED" , not settings .DEBUG )}
19+ csrf_header_name = settings .CSRF_HEADER_NAME
20+
21+ if csrf_header_name .startswith ("HTTP_" ):
22+ csrf_header_name = settings .CSRF_HEADER_NAME [5 :]
23+
24+ csrf_header_name = csrf_header_name .replace ("_" , "-" )
25+
26+ return {
27+ "MINIFIED" : get_setting ("MINIFIED" , not settings .DEBUG ),
28+ "CSRF_HEADER_NAME" : csrf_header_name ,
29+ }
2030
2131
2232@register .inclusion_tag ("unicorn/errors.html" , takes_context = True )
Original file line number Diff line number Diff line change 1+ import test from "ava" ;
2+ import { init } from "../../../django_unicorn/static/js/unicorn.js" ;
3+
4+ test ( "init unicorn" , ( t ) => {
5+ const actual = init ( "unicorn/" , "X-Unicorn" ) ;
6+
7+ t . true ( actual . messageUrl === "unicorn/" ) ;
8+ t . true ( actual . csrfTokenHeaderName === "X-Unicorn" ) ;
9+ } ) ;
Original file line number Diff line number Diff line change 1+ from django_unicorn .templatetags .unicorn import unicorn_scripts
2+
3+
4+ def test_unicorn_scripts ():
5+ actual = unicorn_scripts ()
6+
7+ assert actual ["CSRF_HEADER_NAME" ] == "X-CSRFTOKEN"
8+ assert actual ["MINIFIED" ] is True
9+
10+
11+ def test_unicorn_scripts_debug (settings ):
12+ settings .DEBUG = True
13+ actual = unicorn_scripts ()
14+
15+ assert actual ["CSRF_HEADER_NAME" ] == "X-CSRFTOKEN"
16+ assert actual ["MINIFIED" ] is False
17+
18+
19+ def test_unicorn_scripts_minified_true (settings ):
20+ settings .UNICORN = {"MINIFIED" : True }
21+ actual = unicorn_scripts ()
22+
23+ assert actual ["CSRF_HEADER_NAME" ] == "X-CSRFTOKEN"
24+ assert actual ["MINIFIED" ] is True
25+
26+
27+ def test_unicorn_scripts_minified_false (settings ):
28+ settings .UNICORN = {"MINIFIED" : False }
29+ actual = unicorn_scripts ()
30+
31+ assert actual ["MINIFIED" ] is False
32+
33+
34+ def test_unicorn_scripts_csrf_header_name (settings ):
35+ settings .CSRF_HEADER_NAME = "HTTP_X_UNICORN"
36+ actual = unicorn_scripts ()
37+
38+ assert actual ["CSRF_HEADER_NAME" ] == "X-UNICORN"
You can’t perform that action at this time.
0 commit comments