Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

make test server for proper Ajax tests

  • Loading branch information...
commit 4769efdb44a86adf646243865ff6f67369a9fcad 1 parent bf46bf6
@mislav mislav authored
View
1  .gitignore
@@ -8,3 +8,4 @@ docs/*
.jhw-cache
.rbenv-version
public/
+node_modules
View
36 README.md
@@ -184,26 +184,36 @@ This will submit a pull request to us that we can review.
### Running tests
-Zepto test suite is in "test/\*.html" files. Open the individual files in a web
-browser to run the tests. Files named with "\_functional" are not automated
-tests, but require interaction. Automated tests are written using
-[Evidence.js][].
+~~~ sh
+# run all tests
+$ script/test
-Detailed test information such as specific failures is logged to the JavaScript
-console of your browser.
+# test individual files
+$ script/test test/zepto.html
+~~~
-It's possible to run automated tests from the command-line in headless Webkit
-with [PhantomJS][]:
+You will need <b>node</b> and [PhantomJS][]:
~~~ sh
# install on Mac OS
-$ brew install phantomjs
+$ brew install node phantomjs
-# run all tests
-$ rake test
+# install test prerequisites
+$ npm install
+~~~
+
+Zepto test suite is in "test/\*.html" files. Open the individual files in a web
+browser to run the tests. Files named with "\_functional" are not automated
+tests, but require interaction. Automated tests are written using
+[Evidence.js][].
-# run individual files
-$ script/test test/selector.html
+Ajax tests require a running test server. This is handled automatically through
+`script/test`. Alternatively, you can start the server manually:
+
+~~~ sh
+$ node test/server.js [PORT]
+# Now you can open http://localhost:3000/test/ajax.html in the browser.
+# Terminate the test server with Ctrl-C
~~~
View
20 package.json
@@ -0,0 +1,20 @@
+{
+ "name": "zepto"
+ , "version": "1.0.0"
+ , "homepage": "http://zeptojs.com"
+ , "scripts": { "test": "script/test" }
+ , "repository": {
+ "type": "git"
+ , "url": "https://github.com/madrobby/zepto.git"
+ }
+ , "licenses": [
+ {
+ "type": "MIT"
+ , "url": "http://zeptojs.com/license/"
+ }
+ ]
+ , "devDependencies": {
+ "uglify-js": "1.2.6"
+ , "connect": "2.7.1"
+ }
+}
View
16 script/test
@@ -1,2 +1,14 @@
-#!/bin/sh
-exec phantomjs test/runner.coffee "$@"
+#!/usr/bin/env bash
+port=3999
+pidfile=test/server.pid
+
+node test/server.js $port &
+pid=$!
+
+terminate_test_server() {
+ kill $pid
+}
+trap terminate_test_server EXIT
+
+phantomjs test/runner.coffee "http://localhost:${port}/" "$@"
+exit $?
View
12 test/ajax.html
@@ -61,9 +61,9 @@
testAjaxPost: function(t){
t.pause()
- var xhr = $.post('fixtures/ajax_load_simple.html', function(response){
+ var xhr = $.post('echo', function(response){
deferredResume(t, function(){
- this.assert(response)
+ this.assert(response.match(/^POST/))
})
})
t.assertIn('abort', xhr)
@@ -94,11 +94,13 @@
testAjaxPostWithAcceptType: function(t) {
t.pause()
- $.post('fixtures/ajax_load_simple.html', { sample: 'data' }, function(response) {
+ $.post('echo', { sample: 'data' }, function(response) {
deferredResume(t, function() {
- this.assert(response)
+ this.assert(response.match(/^content-type: application\/x-www-form-urlencoded$/m))
+ this.assert(response.match(/^accept: text\/plain$/m))
+ this.assert(response.match(/^\{"sample":"data"\}\s*$/m))
})
- }, 'text/plain')
+ }, 'text')
},
testAjaxGetJSON: function(t){
View
11 test/runner.coffee
@@ -1,15 +1,18 @@
# Test runner for PhantomJS <phantomjs.org>
# Usage:
-# $ phantomjs test/runner.coffee [<page1>, <page2>, ...]
+# $ phantomjs test/runner.coffee <url-prefix> [<page1>, <page2>, ...]
#
# When no test pages specified, runs all automated tests.
system = require('system')
fs = require('fs')
-if system.args.length > 1
+args = system.args.slice(1)
+prefix = args.shift() || "file://#{fs.workingDirectory}/"
+
+if args.length > 0
# list of test pages to run
- suites = system.args.slice(1)
+ suites = args
else
# by default, run all test/*.html pages
modules = 'zepto ajax data detect event form fx selector stack'.split /\s+/
@@ -41,7 +44,7 @@ loadNextSuite = ->
else
url = suites.shift() + "?verbosity=WARN"
# PhantomJS chokes on the query string on relative paths
- url = "file://#{fs.workingDirectory}/#{url}" if not /:\/\//.test url
+ url = prefix + url if not /:\/\//.test url
page.open url, (status) ->
if status isnt "success"
View
30 test/server.js
@@ -0,0 +1,30 @@
+var connect = require('connect')
+ , parse = connect.utils.parseUrl
+ , mime = connect.utils.mime
+ , http = require('http')
+ , fs = require('fs')
+ , port = process.argv[2] || 3000
+ , app = connect()
+ .use(connect.static(__dirname + '/../')) // serve static files from root dir
+ .use(connect.query())
+ .use(connect.urlencoded())
+ .use(connect.json())
+ .use(function(req, res, next) {
+ var url = parse(req)
+ // provide an echo endpoint for POSTs and such
+ if (url.pathname == "/test/echo") {
+ var body = req.method
+ // echo query params
+ if (req.query) body += ' ?' + JSON.stringify(req.query)
+ // echo request headers
+ body += "\ncontent-type: " + mime(req)
+ body += "\naccept: " + req.headers['accept']
+ // echo request body
+ if (typeof req.body == "string") body += "\n" + req.body
+ else if (req.body) body += "\n" + JSON.stringify(req.body)
+ res.end(body)
+ }
+ else next()
+ })
+
+http.createServer(app).listen(port)
Please sign in to comment.
Something went wrong with that request. Please try again.