-
Notifications
You must be signed in to change notification settings - Fork 16
/
nise.javascript.txt
121 lines (92 loc) · 6.79 KB
/
nise.javascript.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
NISE
ALTERNATIVES ==> # - nise (prefer):
# - more features
# - better XMLHttpRequest mocking
# - jasmine-ajax:
# - Jasmine-specific
VERSION ==> #5.1.4
#Mocks XMLHttpRequest (client-side)
/=+===============================+=\
/ : : \
)==: GENERAL :==(
\ :_______________________________: /
\=+===============================+=/
NISE.fakeXhr.
useFakeXMLHttpRequest()->FAKEXHR #Mocks XMLHttpRequest
NISE.fakeXhr.fakeXMLHttpRequestFor
(WINDOW)->FAKEXHR #Same but for specific WINDOW
NISE.fakeXhr.xhr.XMLHttpRequest #Unmocked XMLHttpRequest
FAKEXHR.restore() #
FAKEXHR.addFilter
(FUNC(...)->BOOL) #Don't mock request made to XHR.open(...) if BOOL true
FAKEXHR.useFilters #BOOL (def: false). Turns on FAKEXHR.addFilter()
/=+===============================+=\
/ : : \
)==: REQUEST :==(
\ :_______________________________: /
\=+===============================+=/
FAKEXHR.onCreate = FUNC(XHR) #When a request is sent.
#Must set up XHR then use XHR.respond(...)
XHR #Same members as real XHR except also ones below
XHR.url #
XHR.responseUrl #
XHR.method #
XHR.requestHeaders #OBJ
XHR.requestBody #STR
XHR.username|password #
XHR.async #BOOL
XHR.getResponseHeader('VAR')->VAL #
XHR.getAllResponseHeaders()->OBJ #
XHR.setStatus(NUM) #
XHR.setResponseHeaders(OBJ) #
XHR.setResponseBody(STR) #
XHR.respond(UINT, OBJ, STR) #Respond to a request. UINT is status code, OBJ is headers, STR is body.
XHR.error() #Simulate a network error.
/=+===============================+=\
/ : : \
)==: RESPONSE :==(
\ :_______________________________: /
\=+===============================+=/
NISE.fakeServer.create([OPTS]) #Calls NISE.fakeXhr.useFakeXMLHttpRequest()
->FAKESERVER #FAKESERVER is a higher-level API above XHR
FAKESERVER.configure(OPTS) #
FAKESERVER.restore() #
NISE.fakeServerWithClock. #Same but using @sinonjs/fake-timers (see its doc), calls:
create(...) # - install() on first XHR.send()
# - uninstall() on either FAKESERVER.respond|restore()
# - CLOCK.tick() to automatically advance any setTimeout|Interval()
FAKESERVER.respondWith #Do XHR.respond() to requests matching METHOD and PATH.
([['METHOD', ]PATH, ]VAL) #Response is according to VAL:
# - [UINT, OBJ, STR]: XHR.respond(...)
# - STR|ARRBUFFER: same as [200, {}, STR|ARRBUFFER]
# - FUNC(XHR):
# - must call XHR.respond()
# - if REGEXP, also pass parenthesis groups as additional arguments
#PATH can be STR|REGEXP or FUNC(STR)->BOOL
#XHR not matching anything will get [404, {}, '']
#Responses are queued until FAKESERVER.respond() is called.
#Must be called before actual requests.
FAKESERVER.respond() #
FAKESERVER.respondAll() #Like FAKESERVER.respond() except can be done again
OPTS|FAKESERVER|XHR.autoRespond #If true (def: false), calls respond() OPTS|FAKESERVER|XHR.autoRespondAfter (def: 10) after XHR.send()
OPTS|FAKESERVER.respondImmediately#If true (def: false), calls respond() right away after XHR.send()
OPTS|FAKESERVER.logger #FUNC(STR) logging request and responses for debugging
OPTS|FAKESERVER.fakeHTTPMethods #If true (def: false), use { _method 'METHOD' } from request payload
#of POST requests when present
/=+===============================+=\
/ : : \
)==: TRACKING :==(
\ :_______________________________: /
\=+===============================+=/
FAKESERVER.requests #XHR_ARR
FAKESERVER.getRequest(NUM)->XHR #
FAKESERVER.first|second|third|
lastRequest #XHR
FAKESERVER.requestCount #NUM
FAKESERVER.requested
[One|Twice|Thrice] #BOOL
FAKESERVER.resetBehavior() #Remove all FAKESERVER.respondWith()
FAKESERVER.resetHistory() #Remove all FAKESERVER.requests
FAKESERVER.reset() #Both