PageRenderTime 42ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 1ms

/resteasy-2.3.2.Final/war-tests/jsapi-servlet-test/src/test/js/test.js

#
JavaScript | 163 lines | 132 code | 28 blank | 3 comment | 0 complexity | 7bdf9c02bad86ea64abe6ab260decd5c MD5 | raw file
  1. var baseURI = 'http://localhost:9095';
  2. Envjs(baseURI+'/test.html', {
  3. scriptTypes : {
  4. "text/javascript" : true
  5. },
  6. logLevel: Envjs.DEBUG
  7. });
  8. REST.loglevel = 1;
  9. function testFunctions() {
  10. assertNotNull("get function", MyResource.get);
  11. }
  12. function testGet() {
  13. var data = MyResource.get();
  14. assertEquals("ok", data);
  15. }
  16. function testGetFoo() {
  17. var data = MyResource.getFoo();
  18. assertEquals("foo", data);
  19. }
  20. function testGetParam() {
  21. var value = "bla";
  22. var data = MyResource.getParam({param: value});
  23. assertEquals(value, data);
  24. }
  25. function testGetFooParam() {
  26. var data = MyResource.getFooParam({param: "paramv", other: "otherv",
  27. q : "qv", c : "cv", h: "hv", m: "mv"});
  28. // cookie is null since env.js doesn't support cookies yet :(
  29. assertEquals("param=paramv;other=otherv;q=qv;c=null;h=hv;m=mv;", data);
  30. }
  31. function testPutFooParam() {
  32. var data = MyResource.putFooParam({param: "paramv", other: "otherv",
  33. q : "qv", c : "cv", h: "hv", m: "mv", $entity: "entityv"});
  34. // cookie is null since env.js doesn't support cookies yet :(
  35. assertEquals("param=paramv;other=otherv;q=qv;c=null;h=hv;m=mv;entity=entityv;", data);
  36. }
  37. function testGetXML() {
  38. var data = MyResource.getXML();
  39. print(data);
  40. assertTrue(data instanceof Document);
  41. var root = data.documentElement;
  42. assertEquals("test", root.nodeName);
  43. assertEquals(1, root.childNodes.length);
  44. assertEquals("var", root.childNodes[0].nodeName);
  45. assertEquals(1, root.childNodes[0].childNodes.length);
  46. assertEquals("foo", root.childNodes[0].childNodes[0].nodeValue);
  47. }
  48. function testGetJSON() {
  49. var data = MyResource.getJSON();
  50. assertEquals(data['var'], "foo");
  51. }
  52. function testGetJSONStarMIME() {
  53. var data = MyResource.getJSONStarMIME();
  54. assertEquals(data['var'], "foo");
  55. }
  56. function testPutJSON() {
  57. var toSend = {'var' : "ok"};
  58. var data = MyResource.putJSON({$entity: toSend});
  59. assertEquals("ok", data);
  60. }
  61. function testPutXML() {
  62. var toSend = document.implementation.createDocument(null, "test", null);
  63. var v = toSend.createElement("var");
  64. toSend.documentElement.appendChild(v);
  65. v.appendChild(toSend.createTextNode("ok"));
  66. var data = MyResource.putXML({$entity: toSend});
  67. assertEquals("ok", data);
  68. }
  69. function testGetMultiRepresentationXML(){
  70. var data = MyResource.getMultiRepresentation({$accepts: "application/xml"});
  71. assertTrue(data instanceof Document);
  72. var root = data.documentElement;
  73. assertEquals(23, root.childNodes.length);
  74. }
  75. function testGetMultiRepresentationJSON(){
  76. var data = MyResource.getMultiRepresentation({$accepts: "application/json"});
  77. assertTrue(data instanceof Array);
  78. assertEquals(23, data.length);
  79. }
  80. function testLookup(){
  81. var value = "foo";
  82. var data = MyResource.lookup({id: value});
  83. assertEquals(baseURI+"/rest/mine/"+value, data);
  84. }
  85. function testSubResource() {
  86. var data = MyResource.getSubResource.get();
  87. assertEquals("Hello", data);
  88. }
  89. function testSubResourceWithPath() {
  90. var data = MyResource.getSubResource.getWithPath();
  91. assertEquals("Hello withPath", data);
  92. }
  93. function testSubResource2() {
  94. var data = MyResource.getSubResource2.get({id: "a", foo: "b"});
  95. assertEquals("Hello a/b", data);
  96. }
  97. function testSubResource2WithPath() {
  98. var data = MyResource.getSubResource2.getWithPath({id: "a", foo: "b", bar: "c"});
  99. assertEquals("Hello withPath a/b/c", data);
  100. }
  101. function testDoubleSubResource() {
  102. var data = MyResource.getSubResource2.getSubResource.get({id: "a"});
  103. assertEquals("Hello", data);
  104. }
  105. function testDoubleSubResourceWithPath() {
  106. var data = MyResource.getSubResource2.getSubResource.getWithPath({id: "a"});
  107. assertEquals("Hello withPath", data);
  108. }
  109. function testForm() {
  110. var data = MyResource.postForm({a: "aa", b: "bb"});
  111. assertEquals("aa/bb", data);
  112. }
  113. // Encoding
  114. function testUTF8(){
  115. assertEquals("%61", REST.Encoding.percentUTF8(0x61));
  116. assertEquals("%ce%91", REST.Encoding.percentUTF8(0x0391));
  117. assertEquals("%e2%89%a2", REST.Encoding.percentUTF8(0x2262));
  118. assertEquals("%f0%a3%8e%b4", REST.Encoding.percentUTF8(0x233B4));
  119. }
  120. function testPercentByte(){
  121. assertEquals("%05", REST.Encoding.percentByte(5));
  122. assertEquals("%20", REST.Encoding.percentByte(32));
  123. }
  124. function testEncoder(){
  125. assertEquals("abc", REST.Encoding.encodeFormNameOrValue("abc"));
  126. assertEquals("%c3%a9", REST.Encoding.encodeFormNameOrValue("?Š"));
  127. assertEquals("%f0%9f%82%84", REST.Encoding.encodeFormNameOrValue("?&#x;‚„"));
  128. }
  129. function testEncoders(){
  130. assertEquals("abc%24%2d%5f%2e%2b%21%2a%27%28%29%2c%2f%3f%26%3d%23+%0D%0A", REST.Encoding.encodeFormNameOrValue("abc$-_.+!*'(),/?&=# \n"));
  131. assertEquals("azAZ09-._~!$&'()*+,%3b=:@%c3%a9%2f%3f%23%5b%5d", REST.Encoding.encodePathParamValue("azAZ09-._~!$&'()*+,;=:@?Š/?#[]"));
  132. assertEquals("azAZ09-._~!$&'()*+,%3b=:@%c3%a9%2f%3f%23%5b%5d", REST.Encoding.encodePathSegment("azAZ09-._~!$&'()*+,;=:@?Š/?#[]"));
  133. assertEquals("azAZ09-._~!$&'()*+,%3b%3d:@%c3%a9%2f%3f%23%5b%5d", REST.Encoding.encodePathParamName("azAZ09-._~!$&'()*+,;=:@?Š/?#[]"));
  134. assertEquals("azAZ09-._~!$%26'()*%2b,;%3d:@%c3%a9/?%23%5b%5d", REST.Encoding.encodeQueryParamNameOrValue("azAZ09-._~!$&'()*+,;=:@?Š/?#[]"));
  135. }