// vim: ts=4:sw=4
var roboHydraHeadDAV = require("../headdav");
exports.getBodyParts = function(conf) {
return {
heads: [
/* base URL, provide default DAV here */
new RoboHydraHeadDAV({ path: "/dav/" }),
/* test cookie:
* POST /dav/testCookieStore will cause the mock server to set a cookie
* GET /dav/testCookieStore will cause the mock server to check the request cookie
* and return 412 Precondition failed when it's not set correctly
*/
new RoboHydraHeadDAV({
path: "/dav/testCookieStore",
handler: function(req,res,next) {
var cookie = 'sess=MY_SESSION_12345';
if (req.method == "POST") {
res.statusCode = 200;
res.headers['Set-Cookie'] = cookie;
res.send("Setting cookie");
} else {
res.statusCode = (req.headers['cookie'] == cookie) ? 200 : 412;
res.send("Checking cookie");
}
}
}),
/* multistatus parsing */
new RoboHydraHeadDAV({
path: "/dav/collection-response-with-trailing-slash",
handler: function(req,res,next) {
if (req.method == "PROPFIND") {
res.statusCode = 207;
res.write('\\
\
\
/dav/collection-response-with-trailing-slash/ \
\
\
\
/principals/ok\
\
\
\
\
\
HTTP/1.1 200 OK\
\
\
\
');
}
}
}),
new RoboHydraHeadDAV({
path: "/dav/collection-response-without-trailing-slash",
handler: function(req,res,next) {
if (req.method == "PROPFIND") {
res.statusCode = 207;
res.write('\\
\
\
/dav/collection-response-without-trailing-slash \
\
\
\
/principals/ok\
\
\
\
\
\
HTTP/1.1 200 OK\
\
\
\
');
}
}
}),
/* principal URL */
new RoboHydraHeadDAV({
path: "/dav/principals/users/test",
handler: function(req,res,next) {
if (req.method == "PROPFIND" && req.rawBody.toString().match(/home-?set/)) {
res.statusCode = 207;
res.write('\\
\
\
/dav/principals/users/t%65st \
\
\
\
/dav/addressbooks/test\
\
\
/dav/calendars/test/\
\
\
HTTP/1.1 200 OK\
\
\
\
');
}
}
}),
/* address-book home set */
new RoboHydraHeadDAV({
path: "/dav/addressbooks/test/",
handler: function(req,res,next) {
if (!req.url.match(/\/$/)) {
res.statusCode = 302;
res.headers['location'] = "/dav/addressbooks/test/";
}
else if (req.method == "PROPFIND" && req.rawBody.toString().match(/addressbook-description/)) {
res.statusCode = 207;
res.write('\\
\
\
/dav/addressbooks/test/useless-member\
\
\
\
\
HTTP/1.1 200 OK\
\
\
\
/dav/addressbooks/test/default-v4.vcf\
\
\
\
\
\
\
Default Address Book\
\
\
\
\
\
HTTP/1.1 200 OK\
\
\
\
https://my.server/absolute:uri/my-address-book\
\
\
\
\
\
\
Absolute URI VCard3 Book\
\
HTTP/1.1 200 OK\
\
\
\
');
}
}
}),
/* calendar home set */
new RoboHydraHeadDAV({
path: "/dav/calendars/test/",
handler: function(req,res,next) {
if (req.method == "PROPFIND" && req.rawBody.toString().match(/calendar-description/)) {
res.statusCode = 207;
res.write('\\
\
\
/dav/calendars/test/shared.forbidden\
\
\
\
\
HTTP/1.1 403 Forbidden\
\
\
\
/dav/calendars/test/private.ics\
\
\
\
\
\
\
Private Calendar\
This is my private calendar.\
\
HTTP/1.1 200 OK\
\
\
\
/dav/calendars/test/work.ics\
\
\
\
\
\
\
\
\
\
Work Calendar\
0xFF00FF\
\
HTTP/1.1 200 OK\
\
\
\
');
}
}
}),
/* non-existing member */
new RoboHydraHeadDAV({
path: "/dav/collection/new.file",
handler: function(req,res,next) {
if (req.method == "PUT") {
if (req.headers['if-match']) /* can't overwrite new file */
res.statusCode = 412;
else {
res.statusCode = 201;
res.headers["ETag"] = "has-just-been-created";
}
} else if (req.method == "DELETE")
res.statusCode = 404;
}
}),
/* existing member */
new RoboHydraHeadDAV({
path: "/dav/collection/existing.file",
handler: function(req,res,next) {
if (req.method == "PUT") {
if (req.headers['if-none-match']) /* requested "don't overwrite", but this file exists */
res.statusCode = 412;
else if (req.headers['if-match'] && req.queryParams && req.queryParams.conflict)
/* requested "don't overwrite", but this file exists with newer content */
res.statusCode = 409;
else {
res.statusCode = 204;
res.headers["ETag"] = "has-just-been-updated";
}
} else if (req.method == "DELETE")
res.statusCode = 204;
}
}),
/* existing member with encoded URL as file name */
new RoboHydraHeadDAV({
path: "/dav/http%253A%252F%252Fwww.invalid.example%252Fm8%252Ffeeds%252Fcontacts%252Fmaria.mueller%252540gmail.com%252Fbase%252F5528abc5720cecc.vcf",
handler: function(req,res,next) {
if (req.method == "GET")
res.statusCode = 200;
}
}),
/* address-book multiget */
new RoboHydraHeadDAV({
path: "/dav/addressbooks/default.vcf/",
handler: function(req,res,next) {
if (req.method == "REPORT" && req.rawBody.toString().match(/addressbook-multiget[\s\S]+[\s\S]+/m &&
req.rawBody.toString().match(/\/dav\/addressbooks\/default\.vcf\/2:3@my%2540pc\.vcf<\/href>/m))) {
res.statusCode = 207;
res.write('\\
\
\
/dav/addressbooks/default.vcf\
\
\
\
My Book\
\
HTTP/1.1 200 OK\
\
\
\
/dav/addressbooks/default.vcf/1.vcf\
\
\
\
BEGIN:VCARD\
VERSION:3.0\
NICKNAME:MULTIGET1\
UID:1\
END:VCARD\
\
\
HTTP/1.1 200 OK\
\
\
\
/dav/addressbooks/default.vcf/2:3%40my%2540pc.vcf\
\
\
\
BEGIN:VCARD\
VERSION:3.0\
NICKNAME:MULTIGET2\
UID:2\
END:VCARD\
\
\
HTTP/1.1 200 OK\
\
\
\
');
}
}
}),
/* address-book multiget where one resource has 404 Not Found */
new RoboHydraHeadDAV({
path: "/dav/addressbooks/default-with-404.vcf/",
handler: function(req,res,next) {
if (req.method == "REPORT" && req.rawBody.toString().match(/addressbook-multiget[\s\S]+[\s\S]+/m &&
req.rawBody.toString().match(/\/dav\/addressbooks\/default-with-404\.vcf\/notexisting<\/href>/m))) {
res.statusCode = 207;
res.write('\\
\
\
/dav/addressbooks/default-with-404.vcf\
\
\
\
\
HTTP/1.1 200 OK\
\
\
\
/dav/addressbooks/default-with-404.vcf/notexisting\
HTTP/1.1 404 Not Found\
\
\
');
}
}
}),
]
};
};