Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

converted http uppercaserer to new workshopper

  • Loading branch information...
commit 1c7c40c7c1b5246b3eed43505c43715742371db6 1 parent edc795b
@rvagg rvagg authored
View
94 exercises/http_uppercaserer/exercise.js
@@ -1 +1,93 @@
-// code stuff here
+var through2 = require('through2')
+ , hyperquest = require('hyperquest')
+ , exercise = require('workshopper-exercise')()
+ , filecheck = require('workshopper-exercise/filecheck')
+ , execute = require('workshopper-exercise/execute')
+ , comparestdout = require('workshopper-exercise/comparestdout')
+ , rndport = require('../../lib/rndport')
+ , words = require('boganipsum/clean_words')
+ .sort(function () { return 0.5 - Math.random() })
+ .slice(0, 10)
+
+
+// checks that the submission file actually exists
+exercise = filecheck(exercise)
+
+// execute the solution and submission in parallel with spawn()
+exercise = execute(exercise)
+
+
+// set up the data file to be passed to the submission
+exercise.addSetup(function (mode, callback) {
+ this.submissionPort = rndport()
+ this.solutionPort = this.submissionPort + 1
+
+ this.submissionArgs = [ this.submissionPort ]
+ this.solutionArgs = [ this.solutionPort ]
+
+ process.nextTick(callback)
+})
+
+
+// add a processor for both run and verify calls, added *before*
+// the comparestdout processor so we can mess with the stdouts
+exercise.addProcessor(function (mode, callback) {
+ this.submissionStdout.pipe(process.stdout)
+
+ // replace stdout with our own streams
+ this.submissionStdout = through2()
+ if (mode == 'verify')
+ this.solutionStdout = through2()
+
+ setTimeout(query.bind(this, mode), 500)
+
+ process.nextTick(function () {
+ callback(null, true)
+ })
+})
+
+
+// compare stdout of solution and submission
+exercise = comparestdout(exercise)
+
+
+// delayed for 500ms to wait for servers to start so we can start
+// playing with them
+function query (mode) {
+ var exercise = this
+
+ function connect (port, stream) {
+ console.log('connect', port)
+ var input = through2()
+ , count = 0
+ , iv
+
+ //TODO: test GET requests for #fail
+ input.pipe(hyperquest.post('http://localhost:' + port)
+ .on('error', function (err) {
+ exercise.emit(
+ 'fail'
+ , 'Error connecting to http://localhost:' + port + ': ' + err.message
+ )
+ }))
+ .pipe(stream)
+
+ iv = setInterval(function () {
+ input.write(words[count].trim() + '\n')
+
+ if (++count == words.length) {
+ clearInterval(iv)
+ input.end()
+ }
+ }, 50)
+
+ }
+
+ connect(this.submissionPort, this.submissionStdout)
+
+ if (mode == 'verify')
+ connect(this.solutionPort, this.solutionStdout)
+}
+
+
+module.exports = exercise
View
53 exercises/http_uppercaserer/setup.js
@@ -1,53 +0,0 @@
-const through = require('through')
- , hyperquest = require('hyperquest')
- , duplexer = require('duplexer')
- , words = require('boganipsum/clean_words')
- .sort(function () { return 0.5 - Math.random() })
- .slice(0, 10)
-
-
-module.exports = function (run) {
- var outputA = through()
- , outputB = through()
- , inputA = through().pause()
- , inputB = through().pause()
- , portA = 1024 + Math.floor(Math.random() * 64511)
- , portB = portA+1
- , count = 0
- , iv
-
- function error (url, out, err) {
- out.write('Error connecting to ' + url + ': ' + err.message)
- out.end()
- }
-
- setTimeout(function () {
- inputA.pipe(hyperquest.post('http://localhost:' + portA)
- .on('error', error.bind(null, 'http://localhost:' + portA, outputA)))
- .pipe(outputA)
- if (!run) {
- inputB.pipe(hyperquest.post('http://localhost:' + portB)
- .on('error', error.bind(null, 'http://localhost:' + portB, outputB)))
- .pipe(outputB)
- }
- }, 500)
-
- iv = setInterval(function () {
- var w = words[count].trim() + '\n'
- inputA.write(w)
- inputB.write(w)
-
- if (++count == words.length) {
- clearInterval(iv)
- inputA.end()
- inputB.end()
- }
- }, 50)
-
- return {
- submissionArgs : [portA]
- , solutionArgs : [portB]
- , a: duplexer(inputA, outputA)
- , b: duplexer(inputB, outputB)
- }
-}
View
13 exercises/http_uppercaserer/solution.js
@@ -1,13 +0,0 @@
-var http = require('http')
-var map = require('through2-map')
-
-var server = http.createServer(function (req, res) {
- if (req.method != 'POST')
- return res.end('send me a POST\n')
-
- req.pipe(map(function (chunk) {
- return chunk.toString().toUpperCase()
- })).pipe(res)
-})
-
-server.listen(Number(process.argv[2]))
View
14 exercises/http_uppercaserer/solution/solution.js
@@ -1 +1,13 @@
-// code stuff here
+var http = require('http')
+var map = require('through2-map')
+
+var server = http.createServer(function (req, res) {
+ if (req.method != 'POST')
+ return res.end('send me a POST\n')
+
+ req.pipe(map(function (chunk) {
+ return chunk.toString().toUpperCase()
+ })).pipe(res)
+})
+
+server.listen(Number(process.argv[2]))
Please sign in to comment.
Something went wrong with that request. Please try again.