mirror of
https://github.com/webodf/WebODF.git
synced 2026-12-01 16:56:05 +01:00
Karma has no support for PUT and DELETE. The simplest workaround is to start as small server and proxy to it for http requests from the tests.
94 lines
2.2 KiB
JavaScript
94 lines
2.2 KiB
JavaScript
/*jslint nomen: true*/
|
|
/*global __dirname, module, require */
|
|
function createPattern(path) {
|
|
"use strict";
|
|
return {pattern: path, included: true, served: true, watched: false};
|
|
}
|
|
|
|
function get(path, response) {
|
|
"use strict";
|
|
var fs = require('fs');
|
|
fs.stat(path, function (err, stats) {
|
|
var s;
|
|
if (err) {
|
|
response.writeHead(404);
|
|
response.end();
|
|
} else {
|
|
response.writeHead(200, { 'content-length': stats.size });
|
|
s = fs.createReadStream(path);
|
|
s.pipe(response);
|
|
}
|
|
});
|
|
}
|
|
|
|
function put(path, request, response) {
|
|
"use strict";
|
|
var fs = require('fs'),
|
|
s = fs.createWriteStream(path);
|
|
request.pipe(s);
|
|
request.on('end', function () {
|
|
response.writeHead(200);
|
|
response.end();
|
|
});
|
|
}
|
|
|
|
function head(path, response) {
|
|
"use strict";
|
|
var fs = require('fs');
|
|
console.log("HRMM " + path);
|
|
fs.stat(path, function (err, stats) {
|
|
console.log("HEAD " + err);
|
|
if (err) {
|
|
response.writeHead(404);
|
|
} else {
|
|
response.writeHead(200, { 'content-length': stats.size });
|
|
}
|
|
response.end();
|
|
});
|
|
}
|
|
|
|
function unlink(path, response) {
|
|
"use strict";
|
|
var fs = require('fs');
|
|
fs.unlink(path, function (err) {
|
|
if (err) {
|
|
response.writeHead(500);
|
|
} else {
|
|
response.writeHead(200);
|
|
}
|
|
response.end();
|
|
});
|
|
}
|
|
|
|
function handleRequest(request, response) {
|
|
"use strict";
|
|
var path = request.url.slice(1);
|
|
console.log(request.method + " " + path);
|
|
if (request.method === 'GET') {
|
|
get(path, response);
|
|
} else if (request.method === 'PUT') {
|
|
put(path, request, response);
|
|
} else if (request.method === 'DELETE') {
|
|
unlink(path, response);
|
|
} else if (request.method === 'HEAD') {
|
|
head(path, response);
|
|
} else {
|
|
response.end(500);
|
|
}
|
|
}
|
|
|
|
function initWebODF(files) {
|
|
"use strict";
|
|
files.unshift(createPattern(__dirname + '/adapter.js'));
|
|
var http = require('http'),
|
|
server = http.createServer(handleRequest);
|
|
server.listen(8642);
|
|
}
|
|
|
|
|
|
initWebODF.$inject = ['config.files'];
|
|
|
|
module.exports = {
|
|
'framework:webodf': ['factory', initWebODF]
|
|
};
|