Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Refactor mock Writable Streams into a module.

  • Loading branch information...
commit 847a525f59c241d51f11eb178bb6a9072fd8cdc5 1 parent 2628d7a
@reid reid authored
Showing with 46 additions and 0 deletions.
  1. +46 −0 test/lib/writable-stream.js
View
46 test/lib/writable-stream.js
@@ -0,0 +1,46 @@
+/*!
+ * YUI Mocha
+ * Copyright 2011 Yahoo! Inc.
+ * Licensed under the BSD license.
+ */
+
+/**
+ * Returns a Vows topic function, which will return
+ * a MockWritableStream, a fake Writable Stream.
+ *
+ * Implements writeHead, like a http.ServerResponse.
+ * Only the (status, headers) signature is implemented.
+ *
+ * Verify with these instance variables:
+ *
+ * - $store: Written data, from write
+ * - $status: HTTP status code, from writeHead
+ * - $headers: HTTP header object, from writeHead
+ *
+ * @return {Function} Vows topic function
+ */
+module.exports = function () {
+ return function () {
+ function MockWritableStream () {
+ this.$store = "";
+ }
+ var proto = MockWritableStream.prototype;
+ proto.writeHead = function (status, /* msg, */ headers) {
+ this.$status = status;
+ this.$headers = headers;
+ };
+ proto.end = function (input) {
+ if (input) this.write(input);
+ this.$end = true;
+ };
+ proto.write = function (input) {
+ if (this.$end) throw new Error("Unable to write: closed.");
+ if (Buffer.isBuffer(input)) {
+ this.$store += input.toString("utf8");
+ } else {
+ this.$store += input;
+ }
+ };
+ return new MockWritableStream;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.