| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.ds.JsXmlHttpDataSourceTest'); |
| 16 |
goog.setTestOnly('goog.ds.JsXmlHttpDataSourceTest'); |
| 17 |
|
| 18 |
goog.require('goog.ds.JsXmlHttpDataSource'); |
| 19 |
goog.require('goog.testing.TestQueue'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
goog.require('goog.testing.net.XhrIo'); |
| 22 |
|
| 23 |
var TEXT_PREFIX = null; |
| 24 |
var TEXT_POSTFIX = null; |
| 25 |
|
| 26 |
var INDEX_OF_URI_ENTRY = 1; |
| 27 |
var INDEX_OF_CONTENT_ENTRY = 3; |
| 28 |
|
| 29 |
function setUp() {} |
| 30 |
|
| 31 |
function tearDown() {} |
| 32 |
|
| 33 |
function testLoad_WithPostAndQueryDataSet() { |
| 34 |
var USE_POST = true; |
| 35 |
var dataSource = new goog.ds.JsXmlHttpDataSource( |
| 36 |
'uri', 'namne', TEXT_PREFIX, TEXT_POSTFIX, USE_POST); |
| 37 |
|
| 38 |
var testQueue = new goog.testing.TestQueue(); |
| 39 |
dataSource.xhr_ = new goog.testing.net.XhrIo(testQueue); |
| 40 |
|
| 41 |
var expectedContent = 'Some test content'; |
| 42 |
dataSource.setQueryData(expectedContent); |
| 43 |
dataSource.load(); |
| 44 |
|
| 45 |
assertFalse(testQueue.isEmpty()); |
| 46 |
|
| 47 |
var actualRequest = testQueue.dequeue(); |
| 48 |
assertEquals(expectedContent, actualRequest[INDEX_OF_CONTENT_ENTRY]); |
| 49 |
assertTrue(testQueue.isEmpty()); |
| 50 |
} |
| 51 |
|
| 52 |
function testLoad_WithPostAndNoQueryDataSet() { |
| 53 |
var USE_POST = true; |
| 54 |
var dataSource = new goog.ds.JsXmlHttpDataSource( |
| 55 |
'uri?a=1&b=2', 'namne', TEXT_PREFIX, TEXT_POSTFIX, USE_POST); |
| 56 |
|
| 57 |
var testQueue = new goog.testing.TestQueue(); |
| 58 |
dataSource.xhr_ = new goog.testing.net.XhrIo(testQueue); |
| 59 |
|
| 60 |
dataSource.load(); |
| 61 |
|
| 62 |
assertFalse(testQueue.isEmpty()); |
| 63 |
|
| 64 |
var actualRequest = testQueue.dequeue(); |
| 65 |
assertEquals('a=1&b=2', actualRequest[INDEX_OF_CONTENT_ENTRY].toString()); |
| 66 |
assertTrue(testQueue.isEmpty()); |
| 67 |
} |
| 68 |
|
| 69 |
function testLoad_WithGet() { |
| 70 |
var USE_GET = false; |
| 71 |
var expectedUri = 'uri?a=1&b=2'; |
| 72 |
var dataSource = new goog.ds.JsXmlHttpDataSource( |
| 73 |
expectedUri, 'namne', TEXT_PREFIX, TEXT_POSTFIX, USE_GET); |
| 74 |
|
| 75 |
var testQueue = new goog.testing.TestQueue(); |
| 76 |
dataSource.xhr_ = new goog.testing.net.XhrIo(testQueue); |
| 77 |
|
| 78 |
dataSource.load(); |
| 79 |
|
| 80 |
assertFalse(testQueue.isEmpty()); |
| 81 |
|
| 82 |
var actualRequest = testQueue.dequeue(); |
| 83 |
assertEquals(expectedUri, actualRequest[INDEX_OF_URI_ENTRY].toString()); |
| 84 |
assertTrue(testQueue.isEmpty()); |
| 85 |
} |