/share/www/script/test/attachment_paths.js

https://github.com/farshidce/jscouchtests · JavaScript · 156 lines · 97 code · 24 blank · 35 comment · 23 complexity · 8221ad819ce35e89ae68abc256a4f159 MD5 · raw file

  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.attachment_paths = function(debug) {
  13. if (debug) debugger;
  14. var dbNames = ["test_suite_db", "test_suite_db/with_slashes"];
  15. for (var i=0; i < dbNames.length; i++) {
  16. var db = new CouchDB(dbNames[i]);
  17. var dbName = encodeURIComponent(dbNames[i]);
  18. db.deleteDb();
  19. db.createDb();
  20. // first just save a regular doc with an attachment that has a slash in the url.
  21. // (also gonna run an encoding check case)
  22. print("first just save a regular doc with an attachment that has a slash in the url.");
  23. var binAttDoc = {
  24. _id: "bin_doc",
  25. _attachments:{
  26. "foo/bar.txt": {
  27. content_type:"text/plain",
  28. data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
  29. },
  30. "foo%2Fbaz.txt": {
  31. content_type:"text/plain",
  32. data: "V2UgbGlrZSBwZXJjZW50IHR3byBGLg=="
  33. }
  34. }
  35. };
  36. print("saving the document");
  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. print("lets try it with an escaped attachment id...");
  43. // weird that it's at two urls
  44. var xhr = CouchDB.request("GET", "/"+dbName+"/bin_doc/foo%2Fbar.txt");
  45. T(xhr.status == 200);
  46. // xhr.responseText == "This is a base64 encoded text"
  47. var xhr = CouchDB.request("GET", "/"+dbName+"/bin_doc/foo/baz.txt");
  48. T(xhr.status == 404);
  49. var xhr = CouchDB.request("GET", "/"+dbName+"/bin_doc/foo%252Fbaz.txt");
  50. T(xhr.status == 200);
  51. T(xhr.responseText == "We like percent two F.");
  52. // require a _rev to PUT
  53. // var xhr = CouchDB.request("PUT", "/"+dbName+"/bin_doc/foo/attachment.txt", {
  54. // headers:{"Content-Type":"text/plain;charset=utf-8"},
  55. // body:"Just some text"
  56. // });
  57. // T(xhr.status == 409);
  58. var xhr = CouchDB.request("PUT", "/"+dbName+"/bin_doc/foo/bar2.txt?rev=" + binAttDoc._rev, {
  59. body:"This is no base64 encoded text",
  60. headers:{"Content-Type": "text/plain;charset=utf-8"}
  61. });
  62. T(xhr.status == 201);
  63. var rev = JSON.parse(xhr.responseText).rev;
  64. binAttDoc = db.open("bin_doc");
  65. T(binAttDoc._attachments["foo/bar.txt"] !== undefined);
  66. T(binAttDoc._attachments["foo%2Fbaz.txt"] !== undefined);
  67. T(binAttDoc._attachments["foo/bar2.txt"] !== undefined);
  68. TEquals("text/plain;charset=utf-8", // thank you Safari
  69. binAttDoc._attachments["foo/bar2.txt"].content_type.toLowerCase(),
  70. "correct content-type"
  71. );
  72. T(binAttDoc._attachments["foo/bar2.txt"].length == 30);
  73. //// now repeat the while thing with a design doc
  74. // first just save a regular doc with an attachment that has a slash in the url.
  75. // (also gonna run an encoding check case)
  76. var binAttDoc = {
  77. _id: "_design/bin_doc",
  78. _attachments:{
  79. "foo/bar.txt": {
  80. content_type:"text/plain",
  81. data: "VGhpcyBpcyBhIGJhc2U2NCBlbmNvZGVkIHRleHQ="
  82. },
  83. "foo%2Fbaz.txt": {
  84. content_type:"text/plain",
  85. data: "V2UgbGlrZSBwZXJjZW50IHR3byBGLg=="
  86. }
  87. }
  88. };
  89. T(db.save(binAttDoc).ok);
  90. var xhr = CouchDB.request("GET", "/"+dbName+"/_design%2Fbin_doc/foo/bar.txt");
  91. T(xhr.responseText == "This is a base64 encoded text");
  92. T(xhr.getResponseHeader("Content-Type") == "text/plain");
  93. // lets try it with an escaped attachment id...
  94. // weird that it's at two urls
  95. var xhr = CouchDB.request("GET", "/"+dbName+"/_design%2Fbin_doc/foo%2Fbar.txt");
  96. T(xhr.responseText == "This is a base64 encoded text");
  97. T(xhr.status == 200);
  98. // err, 3 urls
  99. var xhr = CouchDB.request("GET", "/"+dbName+"/_design/bin_doc/foo%2Fbar.txt");
  100. T(xhr.responseText == "This is a base64 encoded text");
  101. T(xhr.status == 200);
  102. // I mean um, 4 urls
  103. var xhr = CouchDB.request("GET", "/"+dbName+"/_design/bin_doc/foo/bar.txt");
  104. T(xhr.responseText == "This is a base64 encoded text");
  105. T(xhr.status == 200);
  106. var xhr = CouchDB.request("GET", "/"+dbName+"/_design%2Fbin_doc/foo/baz.txt");
  107. T(xhr.status == 404);
  108. var xhr = CouchDB.request("GET", "/"+dbName+"/_design%2Fbin_doc/foo%252Fbaz.txt");
  109. T(xhr.status == 200);
  110. T(xhr.responseText == "We like percent two F.");
  111. // require a _rev to PUT
  112. // var xhr = CouchDB.request("PUT", "/"+dbName+"/_design%2Fbin_doc/foo/attachment.txt", {
  113. // headers:{"Content-Type":"text/plain;charset=utf-8"},
  114. // body:"Just some text"
  115. // });
  116. // T(xhr.status == 409);
  117. var xhr = CouchDB.request("PUT", "/"+dbName+"/_design%2Fbin_doc/foo/bar2.txt?rev=" + binAttDoc._rev, {
  118. body:"This is no base64 encoded text",
  119. headers:{"Content-Type": "text/plain;charset=utf-8"}
  120. });
  121. T(xhr.status == 201);
  122. var rev = JSON.parse(xhr.responseText).rev;
  123. binAttDoc = db.open("_design/bin_doc");
  124. T(binAttDoc._attachments["foo/bar.txt"] !== undefined);
  125. T(binAttDoc._attachments["foo/bar2.txt"] !== undefined);
  126. TEquals("text/plain;charset=utf-8", // thank you Safari
  127. binAttDoc._attachments["foo/bar2.txt"].content_type.toLowerCase(),
  128. "correct content-type"
  129. );
  130. T(binAttDoc._attachments["foo/bar2.txt"].length == 30);
  131. }
  132. };