UNPKG

fluentnode

Version:

Fluent apis for node (based on the concepts used in C#'s FluentSharp

128 lines (123 loc) 4.05 kB
// Generated by CoffeeScript 1.12.7 (function() { var http; require('../../src/fluentnode'); http = require('http'); describe('| node-native | http.GET', function() { var bad_Url, server, test_Data, test_Ip, test_Port, url; test_Port = 4566 + Math.floor((Math.random() * 100) + 1); test_Ip = '127.0.0.1'; test_Data = 'hello from web'; url = "http://" + test_Ip + ":" + test_Port; bad_Url = 'http://aaaa.cccc.aaaa.dddd'; server = null; beforeEach(function(done) { server = http.createServer(null); return server.listen_OnPort_Saying(test_Port, test_Data, (function(_this) { return function() { global.__fluentnode.http.HTTP_GET_TIMEOUT = 30; return done(); }; })(this)); }); afterEach(function(done) { return server.close_And_Destroy_Sockets(function() { global.__fluentnode.http.HTTP_GET_TIMEOUT = 500; return done(); }); }); it('GET_Json, json_GET', function(done) { var data; ''.json_GET.assert_Is(''.GET_Json); data = { a: 42 }; server.respond_With_Object_As_Json(data); return url.json_GET(function(json) { json.assert_Is(data); json.a.assert_Is(42); return done(); }); }); it('json_GET (bad url)', function() { return bad_Url.GET_Json(function(json) { return json.assert_Is({}); }); }); it('json_GET_With_Timeout', function(done) { var data; data = { a: 42 }; server.respond_With_Object_As_Json(data); return url.json_GET_With_Timeout(function(data) { data.assert_Is(data); return done(); }); }); it('json_GET_With_Timeout (bad url)', function(done) { return bad_Url.json_GET_With_Timeout(function(data) { data.assert_Is({}); return done(); }); }); it('http_GET', function(done) { var req; server.respond_With_String_As_Text(test_Data); return req = url.http_GET(function(err, data, res) { assert_Is_Null(err); data.assert_Is_String(); req.assert_Instance_Of(http.ClientRequest); res.assert_Instance_Of(http.IncomingMessage); data.assert_Is(test_Data); return done(); }); }); it('http_GET bad port)', function(done) { return (function() { return "http://127.0.0.1:99999".http_GET(); }).assert_Throws(function(error) { error.message.assert_Contains(' >= 0 and < 65536'); return done(); }); }); it('http_GET_Wait_For_Null', function(done) { url.http_GET_Wait_For_Null(function(err) { assert_Is_Null(err); return done(); }); return 20..wait(function() { return server.respond_With_String_As_Text(null); }); }); it('http_GET_Wait_For_Null (no null is returned from server)', function(done) { var attempts, check; server.respond_With_String_As_Text('123'); attempts = ~~(global.__fluentnode.http.HTTP_GET_TIMEOUT / global.__fluentnode.http.HTTP_GET_DELAY); check = function(err) { err.message.assert_Is("[http_GET_Wait_For_Null] never got a null from server " + url + " after " + attempts + " attempts"); return done(); }; return url.http_GET_Wait_For_Null(check, attempts); }); it('http_GET_With_Timeout', function(done) { var value; value = 'asd'.add_5_Random_Letters(); server.respond_With_String_As_Text(null); url.http_GET_With_Timeout(function(data) { data.assert_Is(value); return done(); }); return 20..wait(function() { return server.respond_With_String_As_Text(value); }); }); return it('http_GET_With_Timeout (null is always returned from server)', function(done) { server.respond_With_String_As_Text(null); return url.http_GET_With_Timeout(function(data) { assert_Is_Null(data); return done(); }); }); }); }).call(this);