aboutsummaryrefslogtreecommitdiffstats
path: root/actionview/test/ujs/public/test/call-remote.js
blob: 8a88471982d8d129cca169a09e8e49ca1f27b289 (plain) (blame)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
(function() {

function buildForm(attrs) {
  attrs = $.extend({ action: '/echo', 'data-remote': 'true' }, attrs)

  $('#qunit-fixture').append($('<form />', attrs))
    .find('form').append($('<input type="text" name="user_name" value="john">'))
}

module('call-remote')

function submit(fn) {
  $('form')
    .bindNative('ajax:success', fn)
    .bindNative('ajax:complete', function() { start() })
    .triggerNative('submit')
}

asyncTest('form method is read from "method" and not from "data-method"', 1, function() {
  buildForm({ method: 'post', 'data-method': 'get' })

  submit(function(e, data, status, xhr) {
    App.assertPostRequest(data)
  })
})

asyncTest('form method is not read from "data-method" attribute in case of missing "method"', 1, function() {
  buildForm({ 'data-method': 'put' })

  submit(function(e, data, status, xhr) {
    App.assertGetRequest(data)
  })
})

asyncTest('form method is read from submit button "formmethod" if submit is triggered by that button', 1, function() {
  var submitButton = $('<input type="submit" formmethod="get">')
  buildForm({ method: 'post' })

  $('#qunit-fixture').find('form').append(submitButton)
    .bindNative('ajax:success', function(e, data, status, xhr) {
      App.assertGetRequest(data)
    })
    .bindNative('ajax:complete', function() { start() })

  submitButton.triggerNative('click')
})

asyncTest('form default method is GET', 1, function() {
  buildForm()

  submit(function(e, data, status, xhr) {
    App.assertGetRequest(data)
  })
})

asyncTest('form url is picked up from "action"', 1, function() {
  buildForm({ method: 'post' })

  submit(function(e, data, status, xhr) {
    App.assertRequestPath(data, '/echo')
  })
})

asyncTest('form url is read from "action" not "href"', 1, function() {
  buildForm({ method: 'post', href: '/echo2' })

  submit(function(e, data, status, xhr) {
    App.assertRequestPath(data, '/echo')
  })
})

asyncTest('form url is read from submit button "formaction" if submit is triggered by that button', 1, function() {
  var submitButton = $('<input type="submit" formaction="/echo">')
  buildForm({ method: 'post', href: '/echo2' })

  $('#qunit-fixture').find('form').append(submitButton)
    .bindNative('ajax:success', function(e, data, status, xhr) {
      App.assertRequestPath(data, '/echo')
    })
    .bindNative('ajax:complete', function() { start() })

  submitButton.triggerNative('click')
})

asyncTest('prefer JS, but accept any format', 1, function() {
  buildForm({ method: 'post' })

  submit(function(e, data, status, xhr) {
    var accept = data.HTTP_ACCEPT
    ok(accept.match(/text\/javascript.+\*\/\*/), 'Accept: ' + accept)
  })
})

asyncTest('JS code should be executed', 1, function() {
  buildForm({ method: 'post', 'data-type': 'script' })

  $('form').append('<input type="text" name="content_type" value="text/javascript">')
  $('form').append('<input type="text" name="content" value="ok(true, \'remote code should be run\')">')

  submit()
})

asyncTest('ecmascript code should be executed', 1, function() {
  buildForm({ method: 'post', 'data-type': 'script' })

  $('form').append('<input type="text" name="content_type" value="application/ecmascript">')
  $('form').append('<input type="text" name="content" value="ok(true, \'remote code should be run\')">')

  submit()
})

asyncTest('execution of JS code does not modify current DOM', 1, function() {
  var docLength, newDocLength
  function getDocLength() {
    return document.documentElement.outerHTML.length
  }

  buildForm({ method: 'post', 'data-type': 'script' })

  $('form').append('<input type="text" name="content_type" value="text/javascript">')
  $('form').append('<input type="text" name="content" value="\'remote code should be run\'">')

  docLength = getDocLength()

  submit(function() {
    newDocLength = getDocLength()
    ok(docLength === newDocLength, 'executed JS should not present in the document')
  })
})

asyncTest('HTML content should be plain-text', 1, function() {
  buildForm({ method: 'post', 'data-type': 'html' })

  $('form').append('<input type="text" name="content_type" value="text/html">')
  $('form').append('<input type="text" name="content" value="<p>hello</p>">')

  submit(function(e, data, status, xhr) {
    ok(data === '<p>hello</ps>', 'returned data should be a plain-text string')
  })
})

asyncTest('XML document should be parsed', 1, function() {
  buildForm({ method: 'post', 'data-type': 'html' })

  $('form').append('<input type="text" name="content_type" value="application/xml">')
  $('form').append('<input type="text" name="content" value="<p>hello</p>">')

  submit(function(e, data, status, xhr) {
    ok(data instanceof Document, 'returned data should be an XML document')
  })
})

asyncTest('accept application/json if "data-type" is json', 1, function() {
  buildForm({ method: 'post', 'data-type': 'json' })

  submit(function(e, data, status, xhr) {
    equal(data.HTTP_ACCEPT, 'application/json, text/javascript, */*; q=0.01')
  })
})

asyncTest('allow empty "data-remote" attribute', 1, function() {
  var form = $('#qunit-fixture').append($('<form action="/echo" data-remote />')).find('form')

  submit(function() {
    ok(true, 'form with empty "data-remote" attribute is also allowed')
  })
})

asyncTest('query string in form action should be stripped in a GET request in normal submit', 1, function() {
  buildForm({ action: '/echo?param1=abc', 'data-remote': 'false' })

  $(document).one('iframe:loaded', function(e, data) {
    equal(data.params.param1, undefined, '"param1" should not be passed to server')
    start()
  })

  $('#qunit-fixture form').triggerNative('submit')
})

asyncTest('query string in form action should be stripped in a GET request in ajax submit', 1, function() {
  buildForm({ action: '/echo?param1=abc' })

  submit(function(e, data, status, xhr) {
    equal(data.params.param1, undefined, '"param1" should not be passed to server')
  })
})

asyncTest('query string in form action should not be stripped in a POST request in normal submit', 1, function() {
  buildForm({ action: '/echo?param1=abc', method: 'post', 'data-remote': 'false' })

  $(document).one('iframe:loaded', function(e, data) {
    equal(data.params.param1, 'abc', '"param1" should be passed to server')
    start()
  })

  $('#qunit-fixture form').triggerNative('submit')
})

asyncTest('query string in form action should not be stripped in a POST request in ajax submit', 1, function() {
  buildForm({ action: '/echo?param1=abc', method: 'post' })

  submit(function(e, data, status, xhr) {
    equal(data.params.param1, 'abc', '"param1" should be passed to server')
  })
})

asyncTest('allow empty form "action"', 1, function() {
  var currentLocation, ajaxLocation

  buildForm({ action: '' })

  $('#qunit-fixture').find('form')
    .bindNative('ajax:beforeSend', function(e, xhr, settings) {
      // Get current location (the same way jQuery does)
      try {
        currentLocation = location.href
      } catch(err) {
        currentLocation = document.createElement( 'a' )
        currentLocation.href = ''
        currentLocation = currentLocation.href
      }
      currentLocation = currentLocation.replace(/\?.*$/, '')

      // Actual location (strip out settings.data that jQuery serializes and appends)
      // HACK: can no longer use settings.data below to see what was appended to URL, as of
      // jQuery 1.6.3 (see http://bugs.jquery.com/ticket/10202 and https://github.com/jquery/jquery/pull/544)
      ajaxLocation = settings.url.replace('user_name=john', '').replace(/&$/, '').replace(/\?$/, '')
      equal(ajaxLocation.match(/^(.*)/)[1], currentLocation, 'URL should be current page by default')

      // Prevent the request from actually getting sent to the current page and
      // causing an error.
      return false
    })
    .triggerNative('submit')

  setTimeout(function() { start() }, 13)
})

asyncTest('sends CSRF token in custom header', 1, function() {
  buildForm({ method: 'post' })
  $('#qunit-fixture').append('<meta name="csrf-token" content="cf50faa3fe97702ca1ae" />')

  submit(function(e, data, status, xhr) {
    equal(data.HTTP_X_CSRF_TOKEN, 'cf50faa3fe97702ca1ae', 'X-CSRF-Token header should be sent')
  })
})

asyncTest('intelligently guesses crossDomain behavior when target URL has a different protocol and/or hostname', 1, function() {

  // Don't set data-cross-domain here, just set action to be a different domain than localhost
  buildForm({ action: 'http://www.alfajango.com' })
  $('#qunit-fixture').append('<meta name="csrf-token" content="cf50faa3fe97702ca1ae" />')

  $('#qunit-fixture').find('form')
    .bindNative('ajax:beforeSend', function(evt, req, settings) {

      equal(settings.crossDomain, true, 'crossDomain should be set to true')

      // prevent request from actually getting sent off-domain
      return false
    })
    .triggerNative('submit')

  setTimeout(function() { start() }, 13)
})

asyncTest('intelligently guesses crossDomain behavior when target URL consists of only a path', 1, function() {

  // Don't set data-cross-domain here, just set action to be a different domain than localhost
  buildForm({ action: '/just/a/path' })
  $('#qunit-fixture').append('<meta name="csrf-token" content="cf50faa3fe97702ca1ae" />')

  $('#qunit-fixture').find('form')
    .bindNative('ajax:beforeSend', function(evt, req, settings) {

      equal(settings.crossDomain, false, 'crossDomain should be set to false')

      // prevent request from actually getting sent off-domain
      return false
    })
    .triggerNative('submit')

  setTimeout(function() { start() }, 13)
})

})()