Skip to content

Commit

Permalink
Fix property read in initServer hook.
Browse files Browse the repository at this point in the history
Add a _testInitServer hook so we can verify that this works in the
http.js tests.
  • Loading branch information
cscott committed Nov 3, 2015
1 parent f7c7a62 commit 41dae76
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 8 deletions.
4 changes: 4 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,10 @@ exports.request = function(options, cb) {
for (var f in serverVars) {
server[f] = serverVars[f];
}
// This hook is for test purposes only.
if (options._testInitServer) {
options._testInitServer(server);
}
cb();
};
return request(source, stream, args, serverVars, initServer).tap(function() {
Expand Down
9 changes: 3 additions & 6 deletions src/node_php_jsserver_class.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ ZEND_END_ARG_INFO()

PHP_METHOD(JsServer, __get) {
TRACE(">");
#if 0 // XXX disabled, since it seems to be corrupting memory.
zval *member;
PARSE_PARAMS(__get, "z/", &member);
convert_to_string(member);
Expand All @@ -101,13 +100,11 @@ PHP_METHOD(JsServer, __get) {
zend_symtable_find(Z_ARRVAL_P(obj->track_vars_array),
Z_STRVAL_P(member), Z_STRLEN_P(member) + 1,
reinterpret_cast<void**>(&retval))) {
*return_value_ptr = EG(uninitialized_zval_ptr);
*return_value_ptr = return_value = EG(uninitialized_zval_ptr);
} else {
*return_value_ptr = *retval;
*return_value_ptr = return_value = *retval;
}
#else
RETVAL_BOOL(true);
#endif
Z_ADDREF_P(return_value);
TRACE("<");
}

Expand Down
31 changes: 29 additions & 2 deletions test/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,18 @@ describe('Feeding POST data from JS to PHP', function() {
]);
};
it('should handle a basic GET request', function() {
var serverOk = false;
return makeServer({
path: '/index.html?abc=def&foo=bar+bat',
}, {
source: 'var_dump($_GET)',
// We have to dereference $_SERVER in order to trigger the
// _testInitServer method.
source: 'var_dump($_SERVER["CONTEXT"] ? $_GET : $_GET)',
_testInitServer: function(server) {
serverOk = (server.REQUEST_METHOD === 'GET' &&
server.REQUEST_URI === '/index.html?abc=def&foo=bar+bat' &&
server.QUERY_STRING === 'abc=def&foo=bar+bat');
},
}).spread(function(phpvalue, output, response) {
should(phpvalue).be.null();
output.should.be.equal([
Expand All @@ -68,9 +76,11 @@ describe('Feeding POST data from JS to PHP', function() {
response.should.be.html();
response.should.have.status(200);
response.should.have.header('x-powered-by');
serverOk.should.be.true();
});
});
it('should handle a basic POST request', function() {
var serverOk = false;
var postData = querystring.stringify({
msg: 'Hello World!',
foo: 'bar bat',
Expand All @@ -83,7 +93,14 @@ describe('Feeding POST data from JS to PHP', function() {
'Content-Length': postData.length,
},
}, {
source: 'var_dump($_POST)',
// We have to dereference $_SERVER in order to trigger the
// _testInitServer method.
source: 'var_dump($_SERVER["CONTEXT"] ? $_POST : $_POST)',
_testInitServer: function(server) {
serverOk = (server.REQUEST_METHOD === 'POST' &&
server.REQUEST_URI === '/post' &&
server.QUERY_STRING === '');
},
}, function(request) {
request.write(postData);
}).spread(function(phpvalue, output, response) {
Expand All @@ -100,9 +117,11 @@ describe('Feeding POST data from JS to PHP', function() {
response.should.be.html();
response.should.have.status(200);
response.should.have.header('x-powered-by');
serverOk.should.be.true();
});
});
it('should handle cookies', function() {
var serverOk = false;
return makeServer({
path: '/cookie/test',
headers: {
Expand All @@ -116,13 +135,20 @@ describe('Feeding POST data from JS to PHP', function() {
}, {
source: [
'call_user_func(function() {',
' # ensure that _testInitServer is triggered',
' $_SERVER["CONTEXT"];',
' # ensure we handle duplicate headers sent from PHP',
' setcookie("a", "b");',
' setcookie("c", "d", 0, "/");',
' var_dump($_COOKIE);',
' return 1;',
'})',
].join('\n'),
_testInitServer: function(server) {
serverOk = (server.REQUEST_METHOD === 'GET' &&
server.REQUEST_URI === '/cookie/test' &&
server.QUERY_STRING === '');
},
}).spread(function(phpvalue, output, response) {
should(phpvalue).be.equal(1);
output.should.be.equal([
Expand All @@ -142,6 +168,7 @@ describe('Feeding POST data from JS to PHP', function() {
'a=b',
'c=d; path=/',
]);
serverOk.should.be.true();
});
});
});

0 comments on commit 41dae76

Please sign in to comment.