Trakkasure / node-expect

An expect-style library for NodeJS
33 stars 3 forks source link

Expect For Node

A stream based Expect utility for interacting with streams.

 var Expect = require('node-expect');
 var socket = new require('net').Socket({type:'tcp4'});
 var parser = new Expect();
 var p = new promise();
 parser.conversation(/connect/i) // Start a conversation matching the connection text from the telnet server in the stream as the trigger.
            .sync()           // The expect rules are synchronous, and run in order.
            .expect(/login/i)     // First expect rule matches the login prompt.
              .send("myuser\n")     // Send user login ID.
            .expect(/password/)   // We are expecting a password prompt next.
              .send("mypassword\n") // Now send the password.
            .branch(/denied/i)    // Branch is a special expect rule, where it will test all consecutive branch definitions to decide which match to follow.
                                  // This is handy when you want to run rules synchronously but have a section of async rules run as one synchronous rule.
              .reset()            // Reset will reset the current conversation thread so that it is as if the first expect in this conversation never started.
                                  // This allows you to restart conversation matching on a synchronous rule set without needing to match the conversation trigger.
            .branch(/\$ /)        // Another branch to match.
              .push()             // Push match back into the stream. The end result is that the next conversation will pick up the match since we are ending this one.
              .emit('connected')  // Emit an event named "connected". Other parts of the program can k
              .handler(p.fulfil)  // Call the fulfil function of the promise.
              .end()              // End this conversation.
       .conversation(/\$ /)       // Start this conversation on a shell prompt. This conversation won't be synchronous.
            .expect(null,true)    // Don't expect anything. The conversation trigger is enough. Just do the action. Then consume this rule, not to run again.
              .send("echo 'I win!'\n")
            .expect(/\$ /)        // Expecting the prompt again.
              .send("exit")
            .expect(/closed/i)
              .handler(           // Call an anonymous handler
               function(){
                 socket.destroy();// Be sure to destroy the socket so node isn't left hanging.
              });
              .end()              // End this conversation.
       .monitor(socket);          // Monitor the socket for input. Also, uses the socket as its output.

 socket.connect(23,'127.0.0.1');

 p.when(function() {
    console.log('We logged in!');
 });

Installation

Clone this repository into your node_modules directory.

Features

TODO

API

Construction

 Expect = require('node-expect');
 parser = new Expect();

Constructing a new Expect() object returns a parser object that is used to start building a set of conversations.

Interaction

The following methods are available for channels:

License

(The MIT License)

Copyright (c) 2011 Brandon Myers trakkasure@gmail.com

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.