Giter VIP home page Giter VIP logo

Comments (23)

dscape avatar dscape commented on July 21, 2024 2

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.

pgte avatar pgte commented on July 21, 2024 1

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.

dscape avatar dscape commented on July 21, 2024

You don't have any nock rules:

nock(url)
  .get(path)
  .reply(...)

You can do nock.recorder.rec() to get those rules.

from nock.

carlosvillu avatar carlosvillu commented on July 21, 2024

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.

carlosvillu avatar carlosvillu commented on July 21, 2024

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.

frosas avatar frosas commented on July 21, 2024

Try with this:

google = nock("http://www.google.com").get('/').reply(200, {response: "ok!"})

from nock.

carlosvillu avatar carlosvillu commented on July 21, 2024

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.

dscape avatar dscape commented on July 21, 2024

@pgte check this out

from nock.

pgte avatar pgte commented on July 21, 2024

Sorry, for some reason I'm not getting emails on any of the nock issues. Let me check this one out.

from nock.

pgte avatar pgte commented on July 21, 2024

@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.

carlosvillu avatar carlosvillu commented on July 21, 2024

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.

pgte avatar pgte commented on July 21, 2024

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.

pgte avatar pgte commented on July 21, 2024

it should print "Hey!" at the end.

from nock.

carlosvillu avatar carlosvillu commented on July 21, 2024

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 :(

from nock.

carlosvillu avatar carlosvillu commented on July 21, 2024

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.

carlosvillu avatar carlosvillu commented on July 21, 2024

BTW, here there is the repo https://github.com/Shopfrogs/Nodify

from nock.

dscape avatar dscape commented on July 21, 2024

Did the tests pass in your local but failed in travis?
If so, it's likely to be a concurrency issue

from nock.

pgte avatar pgte commented on July 21, 2024

@dscape the tests pass in travis, what do you mean?

from nock.

dscape avatar dscape commented on July 21, 2024

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.

pgte avatar pgte commented on July 21, 2024

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.

carlosvillu avatar carlosvillu commented on July 21, 2024

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.

samvloeberghs avatar samvloeberghs commented on July 21, 2024

@pgte 6 year old advice, but still worth gold! Thanks alot

from nock.

lock avatar lock commented on July 21, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.