Sorry if that is not the best to ask this.
But i am trying to use nock to mock the Shopify麓s API, then i have some like this to get all blogs from a shop:
class Blog
constructor: (key, pass, shop) ->
throw new Error 'Blog missing parameters' if not pass? or not key? or not shop?
@options =
host: "http://#{key}:#{pass}@#{shop}.myshopify.com"
port: 80
method: "GET"
path: '/admin/blogs.json'
all: (cb) ->
req = request @options, (res) ->
response = ""
res.setEncoding('utf8')
res.on 'data', (data) ->
response += data
res.on 'end', ->
error = new Error 'Request Error #{res.statusCode}' unless res.statusCode is 200
process.nextTick ->
cb(error, JSON.parse(response))
req.end()
And parts of my testing they are some like this:
describe 'Receive a list of all Blogs', ->
before ->
@fixture = loadFixture 'blog/blogs'
@api = nock "https://#{KEY}:#{PASSWORD}@#{STORE}.myshopify.com"
@blog = new Blog KEY, PASSWORD, STORE
it 'should be possible get a list of all blog like an Array', (done) ->
@api.get('/admin/blogs.json').reply(200, @fixture)
@blog.all (err, blogs) =>
@api.done()
blogs.should.be.an.instanceof Object
done()
Sorry but i dont know what is wrong in my code, because allways get the response from the real server. But i need that my calls they are intercepting by nock to response my @fixture arrays of blogs.
Again sorry for ask that here.
You don't have any nock rules:
nock(url)
.get(path)
.reply(...)
You can do nock.recorder.rec() to get those rules.
hi,
I have a nock rules, you can see that i m doing
before ->
@api = nock "https://#{KEY}:#{PASSWORD}@#{STORE}.myshopify.com"
And then in the it statement i am trying to config the correct rules for the nock instance
@api.get('/admin/blogs.json').reply(200, @fixture)
That is correcto or i m missing something
Thx :)
I am trying an isolate solution with this:
{request} = require 'http'
nock = require 'nock'
google = nock("www.google.com").get('/').reply(200, {response: "ok!"})
req = request {host: 'www.google.com', port: 80, path: '/', method: 'GET'}, (res) ->
response = ''
res.on 'data', (chuck) ->
console.log 'Datos: ', chuck.toString()
response += chuck
res.on 'end', ->
console.log "Respuesta: ", response
req.end()
And that neither work !!! ... Sorry, but i cant understand what is wrong :(
Sorry it's just super hard for me to read coffeescript, I don't understand it
Canonical example, in javascript:
dscape at air in ~/Desktop/nock_test
$ node index.js n
{"ok":true}
dscape at air in ~/Desktop/nock_test
$ cat index.js
var nock = require('nock')
, request = require('request')
;
nock('http://www.google.com')
.get('/')
.reply(200, {ok:true}, {})
;
request.get('http://www.google.com', function (_,_,b) { console.log(b); });
Try with this:
google = nock("http://www.google.com").get('/').reply(200, {response: "ok!"})
Thx at all for the comments !
this is so odd, because i tried the example from @dscape and Works! ( maybe is for use Request module?! )
But then i try this code, from tests of the module:
var google, nock, req, request;
request = require('http').request;
nock = require('nock');
google = nock('http://www.google.com').get('/').reply(200, "Hello World!" );
req = request({
host: 'www.google.com',
port: 80,
path: '/',
}, function(res) {
var response;
response = '';
res.on('data', function(chuck) {
console.log('Datos: ', chuck.toString());
return response += chuck;
});
return res.on('end', function() {
return console.log("Respuesta: ", response);
});
});
req.end();
Maybe there is any kind of problem with the native resquest object ?!
:(
@pgte check this out
Sorry, for some reason I'm not getting emails on any of the nock issues. Let me check this one out.
@carlosvillu This is happening because the auth part of the request ("username:password") should not be part of the host name.
You should mock "https://#{STORE}.myshopify.com" and not with the username and password in the header.
mikeal/request will parse those and include them in the request options.auth.
Hi, thx @pgte for the help
But the thing is that dont work this one:
google = nock('http://www.google.com').get('/').reply(200, "Hello World!" );
I can capture the call to google with Request module but not with the native request object.
Maybe the one is a to many realistic example, but i was trying with more conceptual examples and i could not make to work
I tried the following and it worked:
var nock = require('nock');
nock('http://www.google.com').get('/').reply(200, 'Hey!');
require('http').request({host: 'www.google.com', path:'/'}, function(res) {
console.log('hey!');
res.on('data', function(d) {
console.log('data from google: %s', d);
});
}).end();
Does it not work for you?
it should print "Hey!" at the end.
Hi all,
Ok i found what is the issue here. The problem to dont work the simple test was:
var nock = require('nock');
var request = require('http').request;
// This work!
But in my examples i did
var request = require('http').request;
var nock = require('nock');
// And this dont work !
Ok, that is great, but now i will find a big problem. Because in all examples that i can found in internet, require nock and call to the server s made in the same js file. But my organization is different. I have 3 files to made the test:
// src/blog.js
// Where i have my code who will be testing
var request = require('http').request
//
// You can see at the very first comment that there is a class where i made de request to the server in the all method
//
Later i have my tests, there mainly two files.
// test/common.js
//
// A common file with the common requires for all tests files, where a require nock
//
And the test file for the src where i made the call:
// test/blogTest.js
// Where a have some thing like you can see in the first comment
The thing is that i m requireing request in a file, nockin other, and setup nock麓s rules in other ...
At the end when i start the test, i get this
1) Blog Receive a list of all Blogs should be possible get a list of all blog like an Array:
AssertionError: Mocks not yet satisfied:
GET myShop.myshopify.com/admin/blogs.json
2) Blog Receive a list of all Blogs should call at server to get the blogs:
AssertionError: Mocks not yet satisfied:
GET myShop.myshopify.com/admin/blogs.json
But if you see, i call to @api.done() en the function`s callback :(
Then require('nock') has to happen before your app is initialized.
There is nothing nock can do if you don't, since we need to appear first so that the http.request function gets wrapped.
Thx for all yours help, at the end i cant found the way to make work.
I change to fakeserver and request module, and now seem work. Really i dont know why dont worked, because the match was intercepted and the problem was with the not satisfied Mock :(
Whatever ... thx again!!!
BTW, here there is the repo https://github.com/Shopfrogs/Nodify
Did the tests pass in your local but failed in travis?
If so, it's likely to be a concurrency issue
@dscape the tests pass in travis, what do you mean?
Yeah, The module is testing in 0.4 and 0.6 at the same time, concurrently. I had plenty of issues doing this in nano, so i actually made fakeurls based on the node version that was running
But where is the failed test, I can't find one :/
No dia 03/03/2012, 脿s 00:13, Nuno Job
[email protected]
escreveu:
Yeah, The module is testing in 0.4 and 0.6 at the same time, concurrently. I had plenty of issues doing this in nano, so i actually made fakeurls based on the node version that was running
Reply to this email directly or view it on GitHub:
https://github.com/flatiron/nock/issues/44#issuecomment-4296651
Hi all, really all fails happen only in my local machine, never commit nothing to Travis.
But is true that i had too this kind of fails how say @dscape, but not this time.
Is very odd, because i create a new branch to try make work with Nock, because i like the module麓s API. I think that the problem can be in have separate files. One for create rules and other to call http and make request. BUT ... i am sure that we first call require nock until to call require http. And the problem came from not satisfice all expects. That means that the calls is intercepted but the nock.done() dont happen in the correct place. Maybe was this?
Maybe is your try to replicate the scene with 2 files different files you will find it. Because until now all use that i can found was all in the same file. require nock, require http, create rules and make request.
FYI
thx for all :)
@pgte 6 year old advice, but still worth gold! Thanks alot
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鈥檚 related. Thank you!
Most helpful comment
Sorry it's just super hard for me to read coffeescript, I don't understand it
Canonical example, in javascript: