question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problem trying to intercept a request

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 12 years ago
  • Comments:23 (12 by maintainers)

github_iconTop GitHub Comments

2reactions
dscapecommented, Mar 2, 2012

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); });
1reaction
pgtecommented, Mar 2, 2012

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cypress cy.intercept Problems - Gleb Bahmutov
The command cy. intercept can match requests using a substring, a minimatch, or a regular expression. By default, it intercepts requests ...
Read more >
Intercept HTTP requests - Mozilla - MDN Web Docs
To intercept HTTP requests, use the webRequest API. This API enables you to add listeners for various stages of making an HTTP request....
Read more >
A Practical Guide to Intercepting Network Requests in Cypress
First things first - matching our url​​ When using . intercept() command, there are two main questions that need to be resolved. How...
Read more >
Interceptor not intercepting http requests (Angular 6)
In my case interceptor wasn't getting involved for service calls because I had imported HttpClientModule multiple times, for different ...
Read more >
intercept - Cypress Documentation
Spy and stub network requests and responses. Tip: We recommend you read the Network Requests guide first. All intercepts are automatically cleared before....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found