PageRenderTime 50ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

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

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