Comments (23)
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); });
from nock.
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.
from nock.
You don't have any nock rules:
nock(url)
.get(path)
.reply(...)
You can do nock.recorder.rec()
to get those rules.
from nock.
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 :)
from nock.
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 :(
from nock.
Try with this:
google = nock("http://www.google.com").get('/').reply(200, {response: "ok!"})
from nock.
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 ?!
:(
from nock.
@pgte check this out
from nock.
Sorry, for some reason I'm not getting emails on any of the nock issues. Let me check this one out.
from nock.
@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.
from nock.
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
from nock.
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?
from nock.
it should print "Hey!" at the end.
from nock.
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, nock
in other, and setup nock´s rules in other ...
At the end when i start the test, i get this
-
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 -
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 :(
from nock.
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!!!
from nock.
BTW, here there is the repo https://github.com/Shopfrogs/Nodify
from nock.
Did the tests pass in your local but failed in travis?
If so, it's likely to be a concurrency issue
from nock.
@dscape the tests pass in travis, what do you mean?
from nock.
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
from nock.
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:
#44 (comment)
from nock.
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 :)
from nock.
@pgte 6 year old advice, but still worth gold! Thanks alot
from nock.
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!
from nock.
Related Issues (20)
- [Node 18.12] Class extends value undefined is not a constructor or null HOT 2
- incompatible behaviour introduced with v13.3.2 HOT 17
- Interceptors not being removed in 13.3.2 HOT 2
- Replace npm debug with require('node:util').debuglog HOT 2
- Remove lodash HOT 1
- Application to become a nock maintainer. HOT 5
- Expand function fails on weird GET query params HOT 4
- reqheaders does not work when using an undefined requestBody HOT 3
- Add interceptor at the beginning of list
- Return 500 instead of 404 HOT 4
- Bun support for nock.disableNetConnect() HOT 1
- DNS resolve errors and not mocking a lot of requests HOT 7
- POST request with ReadStream request body does not return a response HOT 10
- Disabled discussions HOT 6
- Improve debug logging HOT 2
- Support Abort Signals HOT 1
- Add JSDoc HOT 8
- Using nock inside a stream pipeline with got causes it to hang forever since Node 20.10.0 HOT 2
- Debug logging broken in beta (14.0.0-beta.5) HOT 1
- Error when code cancels fetch response body stream (14.0.0-beta.6) HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from nock.