Permalink
Browse files

Move to a WEBRick based server for testing

  • Loading branch information...
1 parent 457cc14 commit 7783daaf85ebdd3fb9f00d8055d80be2a2fc73ef @aslakhellesoy committed Mar 12, 2009
Showing with 102 additions and 52 deletions.
  1. +0 −1 .gitignore
  2. +1 −2 README.textile
  3. +1 −1 install_tools.sh
  4. 0 spec/fixtures/a/b/hello
  5. +1 −0 spec/fixtures/a/bla
  6. +39 −0 spec/webdav_server.rb
  7. +23 −26 spec/webdav_spec.js
  8. +37 −22 src/webdav.js
View
@@ -1,4 +1,3 @@
rhino*
jsspec
jslint*
-milton
View
@@ -63,8 +63,7 @@ You need to download some tools. Run ./install_tools.sh to pull them down.
h3. In shell A
<pre>
-cd milton/milton-test
-mvn jetty:run
+ruby spec/webdav_server.rb spec/fixtures
</pre>
h3. In shell B
View
@@ -15,4 +15,4 @@ wget http://jslint4java.googlecode.com/files/jslint4java-1.2.1.zip
unzip jslint4java-1.2.1.zip
rm jslint4java-1.2.1.zip
-svn co svn://milton.ettrema.com/milton/trunk/milton
+sudo gem install webrick-webdav
View
No changes.
View
@@ -0,0 +1 @@
+bla
View
@@ -0,0 +1,39 @@
+#!/usr/bin/ruby
+require 'rubygems'
+require 'webrick'
+require 'webrick/httpservlet/webdavhandler'
+
+# Monkey patch REXML to always nil-indent. The indentation is broken in REXML
+# on Ruby 1.8.6 and even when fixed it confuses OS-X.
+module REXML
+ module Node
+ alias old_to_s to_s
+ def to_s(indent=nil)
+ old_to_s(nil)
+ end
+ end
+end
+
+# http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-talk/223386
+# http://gmarrone.objectblues.net/cgi-bin/wiki/WebDAV_-_Linux_server%2c_Mac_OS_X_client
+module WEBrick
+ module HTTPServlet
+ class WebDAVHandlerVersion2 < WebDAVHandler
+ def do_OPTIONS(req, res)
+ super
+ res["DAV"] = "1,2"
+ end
+
+ def do_LOCK(req, res)
+ res.body << "<XXX-#{Time.now.to_s}/>"
+ end
+ end
+ end
+end
+
+log = WEBrick::Log.new
+log.level = WEBrick::Log::DEBUG if $DEBUG
+serv = WEBrick::HTTPServer.new({:Port => 10080, :Logger => log})
+serv.mount("/", WEBrick::HTTPServlet::WebDAVHandlerVersion2, ARGV[0] || Dir.pwd)
+trap(:INT){ serv.shutdown }
+serv.start
View
@@ -2,41 +2,37 @@ load('jsspec/src/jsspec2.js');
load('spec/env.rhino.js');
load('src/webdav.js');
+function xtest(){}
+
with(jsspec.dsl.TDD) {
-suite('WebDAV')
+suite('WebDAV.Fs')
setup(function() {
})
- test('should do GET', function() {
- var stuff = WebDav.GET('http://localhost:8085/webdav/folder3/stuff.html');
- var expected = "<html><body><h1>stuff.html</h1><ul><li><a href='/webdav/folder3/index.html'>index.html</a><li><a href='/webdav/folder3/stuff.html'>stuff.html</a><li><a href='/webdav/folder3/subfolder1/'>subfolder1</a></ul>rename<form method='POST' action='/webdav/folder3/stuff.html'><input type='text' name='name' value='stuff.html'/><input type='submit'></form></body></html>";
- assertEquals(expected, stuff);
- })
-
- test('should read a file', function() {
- var file = WebDav.Fs.file('http://localhost:8085/webdav/folder3/stuff.html');
- var expected = "<html><body><h1>stuff.html</h1><ul><li><a href='/webdav/folder3/index.html'>index.html</a><li><a href='/webdav/folder3/stuff.html'>stuff.html</a><li><a href='/webdav/folder3/subfolder1/'>subfolder1</a></ul>rename<form method='POST' action='/webdav/folder3/stuff.html'><input type='text' name='name' value='stuff.html'/><input type='submit'></form></body></html>";
- assertEquals(expected, file.read());
+ test('should read cotents of a relative File', function() {
+ var bla = new WebDav.Fs('http://localhost:10080').file('/a/bla');
+ assertEquals('bla', bla.read());
})
-
- test('should do PROPFIND', function() {
- var doc = WebDav.PROPFIND('http://localhost:8085/webdav/folder3');
- assertEquals(4, doc.childNodes.length);
+
+ test('should read cotents of an absolute File', function() {
+ var bla = new WebDav.Fs('http://localhost:10080').file('http://localhost:10080/a/bla');
+ assertEquals('bla', bla.read());
})
- test('should do dir', function() {
- var dir = WebDav.Fs.dir('http://localhost:8085/webdav/folder3');
- assertEquals(4, dir.children().length);
-
- assertEquals('http://localhost:8085/webdav/folder3/', dir.children()[0].url);
- assertEquals('http://localhost:8085/webdav/folder3/index.html', dir.children()[1].url);
- assertEquals('http://localhost:8085/webdav/folder3/stuff.html', dir.children()[2].url);
- assertEquals('http://localhost:8085/webdav/folder3/subfolder1', dir.children()[3].url);
+ test('should list contents of directory', function() {
+ var fs = new WebDav.Fs('http://localhost:10080');
+ var dir = fs.dir('/a');
+
+ assertEquals(3, dir.children().length);
+
+ assertEquals('http://localhost:10080/a', dir.children()[0].url);
+ assertEquals('http://localhost:10080/a/b/', dir.children()[1].url);
+ assertEquals('http://localhost:10080/a/bla', dir.children()[2].url);
})
test('should mkdir', function() {
- var dir = WebDav.Fs.dir('http://localhost:8085/webdav/testmkdir');
+ var dir = new WebDav.Fs('http://localhost:10080').dir('/testmkdir');
try {
dir.rm();
} catch(e) {
@@ -53,10 +49,11 @@ suite('WebDAV')
assertEquals(1, dir.children().length);
})
- test('should create file', function() {
- var file = WebDav.Fs.file('http://localhost:8085/webdav/foo.txt');
+ xtest('should create file', function() {
+ var file = WebDav.Fs.file('http://localhost:10080/a/new.txt');
file.write("Some\nFile data");
assertEquals("Some\nFile data", file.read());
+ file.rm();
})
run();
View
@@ -1,26 +1,26 @@
// A raw WebDAV interface
var WebDav = {
GET: function(url, callback) {
- return this.request('GET', url, null, 'text', callback);
+ return this.request('GET', url, {}, null, 'text', callback);
},
PROPFIND: function(url, callback) {
- return this.request('PROPFIND', url, null, 'xml', callback);
+ return this.request('PROPFIND', url, {Depth: "1"}, null, 'xml', callback);
},
MKCOL: function(url, callback) {
- return this.request('MKCOL', url, null, 'text', callback);
+ return this.request('MKCOL', url, {}, null, 'text', callback);
},
DELETE: function(url, callback) {
- return this.request('DELETE', url, null, 'text', callback);
+ return this.request('DELETE', url, {}, null, 'text', callback);
},
PUT: function(url, data, callback) {
- return this.request('PUT', url, data, 'text', callback);
+ return this.request('PUT', url, {}, data, 'text', callback);
},
- request: function(verb, url, data, type, callback) {
+ request: function(verb, url, headers, data, type, callback) {
var xhr = new XMLHttpRequest();
var body = function() {
var b = xhr.responseText;
@@ -45,6 +45,9 @@ var WebDav = {
}
xhr.open(verb, url, async);
xhr.setRequestHeader("Content-Type", "text/xml; charset=UTF-8");
+ for (var header in headers) {
+ xhr.setRequestHeader(header, headers[header]);
+ }
xhr.send(data);
if(!async) {
@@ -54,27 +57,37 @@ var WebDav = {
};
// An Object-oriented API around WebDav.
-WebDav.Fs = {
- file: function(url) {
- this.url = url;
+WebDav.Fs = function(rootUrl) {
+ var fs = this;
+
+ this.file = function(href) {
+ if(/^http/.test(href)) {
+ this.url = href;
+ } else {
+ this.url = rootUrl + href;
+ }
this.read = function(callback) {
- return WebDav.GET(url, callback);
+ return WebDav.GET(this.url, callback);
};
this.write = function(data, callback) {
- return WebDav.PUT(url, data, callback);
+ return WebDav.PUT(this.url, data, callback);
};
this.rm = function(callback) {
- return WebDav.DELETE(url, callback);
+ return WebDav.DELETE(this.url, callback);
};
return this;
- },
+ };
- dir: function(url) {
- this.url = url;
+ this.dir = function(href) {
+ if(/^http/.test(href)) {
+ this.url = href;
+ } else {
+ this.url = rootUrl + href;
+ }
this.children = function(callback) {
var childrenFunc = function(doc) {
@@ -91,31 +104,33 @@ WebDav.Fs = {
var collection = resourcetype.getElementsByTagName('D:collection')[0];
if(collection) {
- result[i] = new WebDav.Fs.dir(href);
+ result[i] = new fs.dir(href);
} else {
- result[i] = new WebDav.Fs.file(href);
+ result[i] = new fs.file(href);
}
}
return result;
};
if(callback) {
- WebDav.PROPFIND(url, function(doc) {
+ WebDav.PROPFIND(this.url, function(doc) {
callback(childrenFunc(doc));
});
} else {
- return childrenFunc(WebDav.PROPFIND(url));
+ return childrenFunc(WebDav.PROPFIND(this.url));
}
};
this.rm = function(callback) {
- return WebDav.DELETE(url, callback);
+ return WebDav.DELETE(this.url, callback);
};
this.mkdir = function(callback) {
- return WebDav.MKCOL(url, callback);
+ return WebDav.MKCOL(this.url, callback);
};
return this;
- }
+ };
+
+ return this;
};

0 comments on commit 7783daa

Please sign in to comment.