Closed earllapura closed 5 years ago
Hey,
So, we've got two problems: One is failing to intercept in your own code and failing Mitm.js's tests. When you ran Mitm.js's, did you install its [development] dependencies, too? It kind of reads like Node is loading your own app's dependencies.
Secondly, do you have a small example of code from your own tests that's failing that led you to create this issue in the first place?
Thanks for replying.
Yes, when I cloned your repository and ran the test (I changed the package.json a bit to make it run using mocha), it passed except those of callback delays, which is irrelevant to my bug. The bug is that the mock socket does not return the expected value.
This is the error message:
$ npm test
> @ test C:\my-project
> mocha
SmsServerBridge
null
1) shouldConnectToServer
0 passing (15ms)
1 failing
1) SmsServerBridge
shouldConnectToServer:
TypeError: Cannot read property 'should' of null
at Context.<anonymous> (test\SmsServerBridgeTest.js:23:22)
The test file is:
const should = require('chai').should();
var server = require('../lib/SmsServerBridge');
const net = require('net');
describe('SmsServerBridge', function() {
var Mitm = require('mitm'), mitm;
beforeEach(function(){
mitm = Mitm();
});
afterEach(function(){
mitm.disable();
});
it('shouldConnectToServer', function(){
mitm.on("connection", function(socket) { socket.write("Hello back!") });
var socket = net.connect(22, "example.org");
// var serverIn = server(socket, {device: {name: "device"}});
socket.write("hello");
socket.setEncoding("utf8")
console.log(socket.read());
socket.read().should.equal({device:'hello'});
});
});
Ah, I think the issue could be that socket.read
only returns data in either the next tick or when it fires the data
event:
process.nextTick(function() { console.log(socket.read()) })
// Or
socket.on("data", console.log)
I see my examples in README assume pure synchrony. That worked with earlier Node versions, but somewhere around maybe v6 or v8 it changed to requiring a tick or more. I'll update the README immediately!
Yes, when I cloned your repository and ran the test (I changed the package.json a bit to make it run using mocha),
How come? Does Make not run on your system?
I don't particularly want those tests to remain failing either. You don't happen to have another Node version around to try, do you? Travis CI tests Mitm.js on v8.11 and v8.11 and those work. I wonder if it's something in v8.9.4...
@moll Thank you. It worked now. That is so dumb of me to forget that the whole thing is async.
How come? Does Make not run on your system?
I used Git for Windows Bash, with added make
utility. For some reason _mocha
in node_modules/bin
could not run, same bug as in this issue. Cygwin also displays the same error. Here is the error:
$ npm test
> mitm@1.4.0 test C:\projects\node-mitm
> make test
C:\projects\node-mitm\node_modules\.bin\_mocha:2
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
^^^^^^^
SyntaxError: missing ) after argument list
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:607:28)
at Object.Module._extensions..js (module.js:654:10)
at Module.load (module.js:556:32)
at tryModuleLoad (module.js:499:12)
at Function.Module._load (module.js:491:3)
at Function.Module.runMain (module.js:684:10)
at startup (bootstrap_node.js:187:16)
at bootstrap_node.js:608:3
make: *** [Makefile:9: test] Error 1
npm ERR! Test failed. See above for more details.
I think that's because node_modules/.bin/_mocha
on Windows seems to a shell, not a JavaScript file, and my Makefile
is invoking it with Node. I don't have Windows around to test, so do you mind pasting its contents somewhere (like http://gist.github.com)? I'm curious to see what it does. Thx.
Found an example of it in the issue you linked to, too. Yeah, I don't think I can fix that in any convenient way. I find it's useful to be able to pass a path to Node.js via the Makefile
, but given that those bin
files on Windows aren't JavaScript files and hard-code Node's executable, that'll never work.
For now, if you want to run those tests through Make on Windows, you'll have to unset the default NODE variable like such make spec NODE=
. That'll invoke _mocha
without passing it to Node.js. Sorry about that.
We can continue chatting, but I'll close the issue as we've covered the actionable parts already. :)
I tried to use
net
to mock a socket and i found out that it was not intercepting. I haveNode
v8.9.4 andmitm
v1.4.0 . Mypackage.json
hasI ran the tests in the
mitm
folder in./node_modules
and the results are:What happened?