PageRenderTime 54ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/test/javascript/tests/attachment_paths.js

http://github.com/apache/couchdb
JavaScript | 156 lines | 107 code | 24 blank | 25 comment | 25 complexity | 6f0c8f44054e34e06da82c03b5e62ae2 MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0
  1. // Licensed under the Apache License, Version 2.0 (the "License"); you may not
  2. // use this file except in compliance with the License. You may obtain a copy of
  3. // the License at
  4. //
  5. // http://www.apache.org/licenses/LICENSE-2.0
  6. //
  7. // Unless required by applicable law or agreed to in writing, software
  8. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  9. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  10. // License for the specific language governing permissions and limitations under
  11. // the License.
  12. couchTests.elixir = true;
  13. couchTests.attachment_paths = function(debug) {
  14. return console.log('done in test/elixir/test/attachment_paths_test.exs');
  15. if (debug) debugger;
  16. var r_db_name = get_random_db_name()
  17. var dbNames = [r_db_name, r_db_name + "/with_slashes"];
  18. for (var i=0; i < dbNames.length; i++) {
  19. var db = new CouchDB(dbNames[i]);
  20. var dbName = encodeURIComponent(dbNames[i]);
  21. db.createDb();
  22. // first just save a regular doc with an attachment that has a slash in the url.
  23. // (also gonna run an encoding check case)
  24. var binAttDoc = {
  25. _id: "bin_doc",
  26. _attachments:{
  27. "foo/bar.txt": {
  28. content_type:"text/plain",
  29. data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
  30. },
  31. "foo%2Fbaz.txt": {
  32. content_type:"text/plain",
  33. data: "V2UgbGlrZSBwZXJjZW50IHR3byBGLg=="
  34. }
  35. }
  36. };
  37. T(db.save(binAttDoc).ok);
  38. var xhr = CouchDB.request("GET", "/"+dbName+"/bin_doc/foo/bar.txt");
  39. T(xhr.responseText == "This is a base64 encoded text");
  40. T(xhr.getResponseHeader("Content-Type") == "text/plain");
  41. // lets try it with an escaped attachment id...
  42. // weird that it's at two urls
  43. var xhr = CouchDB.request("GET", "/"+dbName+"/bin_doc/foo%2Fbar.txt");
  44. T(xhr.status == 200);
  45. // xhr.responseText == "This is a base64 encoded text"
  46. var xhr = CouchDB.request("GET", "/"+dbName+"/bin_doc/foo/baz.txt");
  47. T(xhr.status == 404);
  48. var xhr = CouchDB.request("GET", "/"+dbName+"/bin_doc/foo%252Fbaz.txt");
  49. T(xhr.status == 200);
  50. T(xhr.responseText == "We like percent two F.");
  51. // require a _rev to PUT
  52. var xhr = CouchDB.request("PUT", "/"+dbName+"/bin_doc/foo/attachment.txt", {
  53. headers:{"Content-Type":"text/plain;charset=utf-8"},
  54. body:"Just some text"
  55. });
  56. T(xhr.status == 409);
  57. var xhr = CouchDB.request("PUT", "/"+dbName+"/bin_doc/foo/bar2.txt?rev=" + binAttDoc._rev, {
  58. body:"This is no base64 encoded text",
  59. headers:{"Content-Type": "text/plain;charset=utf-8"}
  60. });
  61. T(xhr.status == 201);
  62. var rev = JSON.parse(xhr.responseText).rev;
  63. binAttDoc = db.open("bin_doc");
  64. T(binAttDoc._attachments["foo/bar.txt"] !== undefined);
  65. T(binAttDoc._attachments["foo%2Fbaz.txt"] !== undefined);
  66. T(binAttDoc._attachments["foo/bar2.txt"] !== undefined);
  67. TEquals("text/plain;charset=utf-8", // thank you Safari
  68. binAttDoc._attachments["foo/bar2.txt"].content_type.toLowerCase(),
  69. "correct content-type"
  70. );
  71. T(binAttDoc._attachments["foo/bar2.txt"].length == 30);
  72. //// now repeat the while thing with a design doc
  73. // first just save a regular doc with an attachment that has a slash in the url.
  74. // (also gonna run an encoding check case)
  75. var binAttDoc = {
  76. _id: "_design/bin_doc",
  77. _attachments:{
  78. "foo/bar.txt": {
  79. content_type:"text/plain",
  80. data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
  81. },
  82. "foo%2Fbaz.txt": {
  83. content_type:"text/plain",
  84. data: "V2UgbGlrZSBwZXJjZW50IHR3byBGLg=="
  85. }
  86. }
  87. };
  88. T(db.save(binAttDoc).ok);
  89. var xhr = CouchDB.request("GET", "/"+dbName+"/_design%2Fbin_doc/foo/bar.txt");
  90. T(xhr.responseText == "This is a base64 encoded text");
  91. T(xhr.getResponseHeader("Content-Type") == "text/plain");
  92. // lets try it with an escaped attachment id...
  93. // weird that it's at two urls
  94. var xhr = CouchDB.request("GET", "/"+dbName+"/_design%2Fbin_doc/foo%2Fbar.txt");
  95. T(xhr.responseText == "This is a base64 encoded text");
  96. T(xhr.status == 200);
  97. // err, 3 urls
  98. var xhr = CouchDB.request("GET", "/"+dbName+"/_design/bin_doc/foo%2Fbar.txt");
  99. T(xhr.responseText == "This is a base64 encoded text");
  100. T(xhr.status == 200);
  101. // I mean um, 4 urls
  102. var xhr = CouchDB.request("GET", "/"+dbName+"/_design/bin_doc/foo/bar.txt");
  103. T(xhr.responseText == "This is a base64 encoded text");
  104. T(xhr.status == 200);
  105. var xhr = CouchDB.request("GET", "/"+dbName+"/_design%2Fbin_doc/foo/baz.txt");
  106. T(xhr.status == 404);
  107. var xhr = CouchDB.request("GET", "/"+dbName+"/_design%2Fbin_doc/foo%252Fbaz.txt");
  108. T(xhr.status == 200);
  109. T(xhr.responseText == "We like percent two F.");
  110. // require a _rev to PUT
  111. var xhr = CouchDB.request("PUT", "/"+dbName+"/_design%2Fbin_doc/foo/attachment.txt", {
  112. headers:{"Content-Type":"text/plain;charset=utf-8"},
  113. body:"Just some text"
  114. });
  115. T(xhr.status == 409);
  116. var xhr = CouchDB.request("PUT", "/"+dbName+"/_design%2Fbin_doc/foo/bar2.txt?rev=" + binAttDoc._rev, {
  117. body:"This is no base64 encoded text",
  118. headers:{"Content-Type": "text/plain;charset=utf-8"}
  119. });
  120. T(xhr.status == 201);
  121. var rev = JSON.parse(xhr.responseText).rev;
  122. binAttDoc = db.open("_design/bin_doc");
  123. T(binAttDoc._attachments["foo/bar.txt"] !== undefined);
  124. T(binAttDoc._attachments["foo/bar2.txt"] !== undefined);
  125. TEquals("text/plain;charset=utf-8", // thank you Safari
  126. binAttDoc._attachments["foo/bar2.txt"].content_type.toLowerCase(),
  127. "correct content-type"
  128. );
  129. T(binAttDoc._attachments["foo/bar2.txt"].length == 30);
  130. db.deleteDb();
  131. }
  132. };