Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Trying to get it working with restify's clients #79

Closed
cjroebuck opened this issue Oct 31, 2012 · 8 comments
Closed

Trying to get it working with restify's clients #79

cjroebuck opened this issue Oct 31, 2012 · 8 comments

Comments

@cjroebuck
Copy link

Can't seem to get it working with restify's various clients, but mainly the json client. Below test shows the nock stub working fine with a plain http.request call, but in the second attempt -- using the same url/port and method -- the test hangs when making the request..

 http = require('http')
 nock = require 'nock'
 restify = require 'restify'

describe "Test mocks using nock", ->

it "should hit the mock when doing a POST to google.com using plain http.request", (done) ->
  nock.cleanAll()
  # nock.recorder.rec()

scope = nock('http://www.google.com:1234')
  .post('/')
  .reply 200, 
    hello:'hello world'
    id: 1234

req = http.request 
  host: 'www.google.com'
  method: 'POST'
  path: '/'
  port: 1234
, (res) ->
  res.on 'data', (chunk) ->
    console.log 'data1: ' + chunk
  res.on 'end', () ->
    scope.done()
    done()

req.end()

it "should hit the mock when doing a POST to google.com using restify json client", (done) ->
 # nock.recorder.rec()
 nock.cleanAll()

scope = nock('http://www.google.com:1234/')
  .defaultReplyHeaders 
    'Content-Type':'application/json'
  .post('/')
  .reply 200, { hello: 'hello universe' }

api_client = restify.createJsonClient
  url: 'http://www.google.com:1234'
  version: '*'

api_client.post '/', (err,req,res,data) ->
  console.log 'data: '+ data
  scope.done()
  done()

console output:

Test mocks using nock
◦ should hit the mock when doing a POST to google.com using plain http.request: data1:      {"hello":"hello world","id":1234}
✓ should hit the mock when doing a POST to google.com using plain http.request 
1) should hit the mock when doing a POST to google.com using restify json client


 ✖ 1 of 2 tests failed:

 1) Test mocks using nock should hit the mock when doing a POST to google.com using restify json client:
 Error: timeout of 4000ms exceeded
  at Object.<anonymous> (/usr/local/lib/node_modules/mocha/lib/runnable.js:158:14)
  at Timer.list.ontimeout (timers.js:101:19)
@pgte
Copy link
Member

pgte commented Oct 31, 2012

The intereceptors are removed once they are "used".
Please check this: https://github.com/flatiron/nock#read-this
And please reopen if this is not the case.

@pgte pgte closed this as completed Oct 31, 2012
@cjroebuck
Copy link
Author

I setup the nock interceptor before each test so don't think it's that.

It's something to do with how restify modifies the node http request object, it's probably out of the scope of nock to try and fix it.

@pgte
Copy link
Member

pgte commented Oct 31, 2012

Yeah, you're right, sorry my coffeescript reading is bad. Reopening this one just to make sure if it's not a nock bug.

You can add a .log(console.log) to the scope like this to see what is going on under the hood:
https://github.com/flatiron/nock#logging

@pgte pgte reopened this Oct 31, 2012
@pgte
Copy link
Member

pgte commented Nov 21, 2012

No feedback, so I'm closing this one.
Please reopen once you have more info.

@pgte pgte closed this as completed Nov 21, 2012
@jasonav
Copy link

jasonav commented Feb 19, 2013

I'm having the same problem. Nock is not working for the restify json client. I tried adding .log(console.log) but nothing was logged. It just hangs. You should be able to repro this quite easily.

@pgte
Copy link
Member

pgte commented Feb 19, 2013

You can inspect what nock is trying to do by putting a callback using .log()

If that doesn't get you an insight, I'll need a failing test case (a gist is fine).

Pedro

On Tuesday, 19 de February de 2013 at 07:40, Jason Avinger wrote:

I'm having the same problem. Nock is not working for the restify json client. I tried adding .log(console.log) but nothing was logged. It just hangs. You should be able to repro this quite easily.


Reply to this email directly or view it on GitHub (#79 (comment)).

@lblazecki
Copy link

Nock can't be used with Restify now.
Problem is when Nock is mocking http.request (or https) and returning what is defined in nock.response.
It is based on assumption that clientRequester is calling something like this :

var moduleCallback;
var req = http.request(options, function (res) {
moduleCallback();
});
req.end();

Nock knows when req.end is called.
But Restify (see http_client.js) is calling :
var moduleCallback;
var req = http.request(opts, function onResponse(res) {
req.emit('result', (err || null), res);
});
req.once('socket', function onSocket(socket) {
req.once('result', function () {
moduleCallback();
req.end();
})
});
So socket must be emitted before callback from http.request is called.
I don't know how this can be done. (I don't know how to access req object or attach new socket to it)

@lock
Copy link

lock bot commented Sep 14, 2018

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue and add a reference to this one if it’s related. Thank you!

@lock lock bot locked as resolved and limited conversation to collaborators Sep 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants