1
+ describe ( 'headers' , function ( ) {
2
+ var module = window . __module , http ;
3
+
4
+ beforeAll ( function ( ) {
5
+ http = window . __getHttp ( ) ;
6
+ } ) ;
7
+
8
+ it ( 'matches a request by headers' , function ( done ) {
9
+ http ( {
10
+ method : 'get' ,
11
+ url : 'my-api.com/user' ,
12
+ headers : {
13
+ 'x-auth' : 'pass' ,
14
+ 'gzip-pro' : 'yes'
15
+ }
16
+ } ) . then ( function ( response ) {
17
+ expect ( response . data ) . toBe ( 'authentication passed' ) ;
18
+ done ( ) ;
19
+ } ) ;
20
+ } ) ;
21
+
22
+ it ( 'can respond with headers' , function ( done ) {
23
+ http ( {
24
+ url : 'my-api.com/with-headers' ,
25
+ method : 'get'
26
+ } ) . then ( function ( response ) {
27
+ expect ( response . headers ( 'X-AUTH' ) ) . toBe ( 'authenticated' ) ;
28
+ expect ( response . headers ( 'ANGULAR_API' ) ) . toBe ( 'ang=api' ) ;
29
+ done ( ) ;
30
+ } ) ;
31
+ } ) ;
32
+
33
+ it ( 'can respond with headers in convenience methods' , function ( done ) {
34
+ http ( {
35
+ url : 'my-api.com/with-headers' ,
36
+ method : 'get'
37
+ } ) . success ( function ( data , status , headers ) {
38
+ expect ( headers ( 'X-AUTH' ) ) . toBe ( 'authenticated' ) ;
39
+ expect ( headers ( 'ANGULAR_API' ) ) . toBe ( 'ang=api' ) ;
40
+ done ( ) ;
41
+ } ) ;
42
+ } ) ;
43
+
44
+ it ( 'ignores header properties when their function return value is null' , function ( done ) {
45
+ http ( {
46
+ method : 'get' ,
47
+ url : 'my-api.com/user' ,
48
+ headers : {
49
+ 'x-auth' : 'pass' ,
50
+ 'gzip-pro' : 'yes' ,
51
+ 'ignore-me' : function ( ) {
52
+ return null ;
53
+ }
54
+ }
55
+ } ) . then ( function ( response ) {
56
+ expect ( response . data ) . toBe ( 'authentication passed' ) ;
57
+ done ( ) ;
58
+ } ) ;
59
+ } ) ;
60
+
61
+ it ( 'always returns a header' , function ( done ) {
62
+ http ( {
63
+ method : 'GET' ,
64
+ url : 'test-api.com/user'
65
+ } ) . then ( function ( response ) {
66
+ expect ( response . headers ) . toBeDefined ( ) ;
67
+ expect ( response . data ) . toBe ( 'pass' ) ;
68
+ done ( ) ;
69
+ } ) ;
70
+ } ) ;
71
+
72
+ it ( 'matches request by complex headers (include functions)' , function ( done ) {
73
+ http ( {
74
+ method : 'get' ,
75
+ url : 'my-api.com/user' ,
76
+ headers : {
77
+ 'x-auth' : 'pass' ,
78
+ 'gzip-pro' : function ( config ) {
79
+ if ( config ) {
80
+ return 'yes' ;
81
+ }
82
+ }
83
+ }
84
+ } ) . then ( function ( response ) {
85
+ expect ( response . data ) . toBe ( 'authentication passed' ) ;
86
+ done ( ) ;
87
+ } ) ;
88
+ } ) ;
89
+
90
+ it ( 'response headers function returns all headers if no provided header name' , function ( done ) {
91
+ var expectedHeaders = {
92
+ 'X-AUTH' : 'authenticated' ,
93
+ 'ANGULAR_API' : 'ang=api'
94
+ } ;
95
+
96
+ http ( {
97
+ url : 'my-api.com/with-headers' ,
98
+ method : 'get'
99
+ } ) . then ( function ( response ) {
100
+ expect ( response . headers ( ) ) . toEqual ( expectedHeaders ) ;
101
+ expect ( response . headers ( null ) ) . toEqual ( expectedHeaders ) ;
102
+ expect ( response . headers ( undefined ) ) . toEqual ( expectedHeaders ) ;
103
+ done ( ) ;
104
+ } ) ;
105
+ } ) ;
106
+ } ) ;
0 commit comments