Closed HairyMike closed 2 years ago
It looks like something in the javascript is causing this issue because the following example using curl shows this is not happening:
$ curl -X PUT 'localhost:1080/mockserver/expectation' \
-d '{
"httpRequest": {
"method": "GET",
"path": "/test"
},
"httpResponse": {
"statusCode": 200,
"reasonPhrase": "OK",
"headers": [
{
"name": "Content-Type",
"values": [
"application/json"
]
}
],
"body": {
"value": 1000000000000100000001
}
}
}'
[ {
"httpRequest" : {
"method" : "GET",
"path" : "/test"
},
"httpResponse" : {
"statusCode" : 200,
"reasonPhrase" : "OK",
"headers" : {
"Content-Type" : [ "application/json" ]
},
"body" : {
"value" : 1000000000000100000001
}
},
"id" : "f971a79d-4c8a-4b8d-a04b-2b31d30e5f6a",
"priority" : 0,
"timeToLive" : {
"unlimited" : true
},
"times" : {
"unlimited" : true
}
} ]% $ curl 'localhost:1080/test'
{
"value" : 1000000000000100000001
}%
and the corresponding MockServer logs:
2022-02-09 12:27:59 5.11.3-SNAPSHOT INFO logger level is INFO, change using:
- 'ConfigurationProperties.logLevel(String level)' in Java code,
- '-logLevel' command line argument,
- 'mockserver.logLevel' JVM system property or,
- 'mockserver.logLevel' property value in 'mockserver.properties'
2022-02-09 12:27:59 5.11.3-SNAPSHOT INFO 1080 started on port: 1080
2022-02-09 12:28:05 5.11.3-SNAPSHOT INFO 1080 creating expectation:
{
"httpRequest" : {
"method" : "GET",
"path" : "/test"
},
"httpResponse" : {
"statusCode" : 200,
"reasonPhrase" : "OK",
"headers" : {
"Content-Type" : [ "application/json" ]
},
"body" : {
"type" : "JSON",
"json" : {
"value" : 1000000000000100000001
}
}
},
"id" : "f971a79d-4c8a-4b8d-a04b-2b31d30e5f6a",
"priority" : 0,
"timeToLive" : {
"unlimited" : true
},
"times" : {
"unlimited" : true
}
}
with id:
f971a79d-4c8a-4b8d-a04b-2b31d30e5f6a
2022-02-09 12:28:11 5.11.3-SNAPSHOT INFO 1080 received request:
{
"method" : "GET",
"path" : "/test",
"headers" : {
"content-length" : [ "0" ],
"User-Agent" : [ "curl/7.77.0" ],
"Host" : [ "localhost:1080" ],
"Accept" : [ "*/*" ]
},
"keepAlive" : true,
"secure" : false
}
2022-02-09 12:28:11 5.11.3-SNAPSHOT INFO 1080 request:
{
"method" : "GET",
"path" : "/test",
"headers" : {
"content-length" : [ "0" ],
"User-Agent" : [ "curl/7.77.0" ],
"Host" : [ "localhost:1080" ],
"Accept" : [ "*/*" ]
},
"keepAlive" : true,
"secure" : false
}
matched expectation:
{
"httpRequest" : {
"method" : "GET",
"path" : "/test"
},
"httpResponse" : {
"statusCode" : 200,
"reasonPhrase" : "OK",
"headers" : {
"Content-Type" : [ "application/json" ]
},
"body" : {
"type" : "JSON",
"json" : {
"value" : 1000000000000100000001
}
}
},
"id" : "f971a79d-4c8a-4b8d-a04b-2b31d30e5f6a",
"priority" : 0,
"timeToLive" : {
"unlimited" : true
},
"times" : {
"unlimited" : true
}
}
2022-02-09 12:28:11 5.11.3-SNAPSHOT INFO 1080 returning response:
{
"statusCode" : 200,
"reasonPhrase" : "OK",
"headers" : {
"Content-Type" : [ "application/json" ]
},
"body" : {
"value" : 1000000000000100000001
}
}
for request:
{
"method" : "GET",
"path" : "/test",
"headers" : {
"content-length" : [ "0" ],
"User-Agent" : [ "curl/7.77.0" ],
"Host" : [ "localhost:1080" ],
"Accept" : [ "*/*" ]
},
"keepAlive" : true,
"secure" : false
}
for action:
{
"statusCode" : 200,
"reasonPhrase" : "OK",
"headers" : {
"Content-Type" : [ "application/json" ]
},
"body" : {
"value" : 1000000000000100000001
}
}
from expectation:
f971a79d-4c8a-4b8d-a04b-2b31d30e5f6a
It looks like node is doing this conversion because when I create a JSON object with a large number it is converted to the exponent form, as follows:
One way to solve this is as follows:
Number.prototype.toFixedSpecial = function(n) {
var str = this.toFixed(n);
if (str.indexOf('e+') === -1)
return str;
// if number is in scientific notation, pick (b)ase and (p)ower
str = str.replace('.', '').split('e+').reduce(function(b, p) {
return b + Array(p - b.length + 2).join(0);
});
if (n > 0)
str += '.' + Array(n + 1).join(0);
return str;
};
var mockServerClient = require('mockserver-client').mockServerClient;
let largeNumber = 1000000000000100000001;
let expectation = {
"httpRequest": {
"method": "GET",
"path": "/test"
},
"httpResponse": {
"statusCode": 200,
"reasonPhrase": "OK",
"headers": [
{
"name": "Content-Type",
"values": [
"application/json"
]
}
],
"body": {
"value": largeNumber.toFixedSpecial(0)
}
}
};
mockServerClient("localhost", 1080)
.mockAnyResponse(expectation)
.then(
function () {
console.log("expectation created");
},
function (error) {
console.log(error);
}
);
thanks for the thorough investigation @jamesdbloom 👍
Describe the issue Large numbers are reduced to their exponent values.
What you are trying to do Running tests where large numbers are returned by mockserver
MockServer version 5.11.2
To Reproduce Setup the following expectation:
How you are running MockServer (i.e maven plugin, docker, etc) Docker
Code you used to create expectations
What error you saw No Error
Expected behaviour I'd expect to get back the request exactly as it's mocked. Instead, we get the exponent version of the large number.
MockServer Log This is kind of long. In my example above, I pulled out only the relevant bits to show the issue. Look for the
httpResponse.total
value to see the exponent version of the value.