File tree 5 files changed +73
-7
lines changed
5 files changed +73
-7
lines changed Original file line number Diff line number Diff line change 1
1
import { Component } from "./component.js" ;
2
- import { isEmpty } from "./utils.js" ;
2
+ import { isEmpty , hasValue } from "./utils.js" ;
3
3
import { components } from "./store.js" ;
4
4
5
5
let messageUrl = "" ;
6
- const csrfTokenHeaderName = "X-CSRFToken" ;
6
+ let csrfTokenHeaderName = "X-CSRFToken" ;
7
7
8
8
/**
9
9
* Initializes the Unicorn object.
10
10
*/
11
- export function init ( _messageUrl ) {
11
+ export function init ( _messageUrl , _csrfTokenHeaderName ) {
12
12
messageUrl = _messageUrl ;
13
+
14
+ if ( hasValue ( _csrfTokenHeaderName ) ) {
15
+ csrfTokenHeaderName = _csrfTokenHeaderName ;
16
+ }
17
+
18
+ return {
19
+ messageUrl,
20
+ csrfTokenHeaderName,
21
+ } ;
13
22
}
14
23
15
24
/**
Original file line number Diff line number Diff line change 5
5
6
6
< script >
7
7
var url = "{% url 'django_unicorn:message' %}" ;
8
- Unicorn . init ( url ) ;
8
+ Unicorn . init ( url , "{{ CSRF_HEADER_NAME }}" ) ;
9
9
</ script >
10
10
{% else %}
11
11
< script type ="module ">
15
15
window . Unicorn = Unicorn ;
16
16
17
17
var url = "{% url 'django_unicorn:message' %}" ;
18
- Unicorn . init ( url ) ;
18
+ Unicorn . init ( url , "{{ CSRF_HEADER_NAME }}" ) ;
19
19
</ script >
20
20
{% endif %}
Original file line number Diff line number Diff line change 13
13
14
14
@register .inclusion_tag ("unicorn/scripts.html" )
15
15
def 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
17
17
from django_unicorn .settings import get_setting
18
18
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
+ }
20
30
21
31
22
32
@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