how to make anyNumber generate number,not string - spring-cloud-contract

response defined in contract dsl is simple as follows:
response { // (6)
status 200 // (7)
body(
number: $(anyNumber())
)
headers { // (9)
contentType('application/json')
}
}
The stub runner boot application returns this:
{"number":"-1559642088"}
But i want the value to be number not string,like this:
{"number":-1559642088}

response { // (6)
status 200 // (7)
body(
number: parseInt($(anyNumber()))
)
headers { // (9)
contentType('application/json')
}
}
use parseInt to convert string to number in javascript

Related

Generate Parser for a File with JavaCC

I am a beginner with JavaCC,and i'm trying to generate a file Parser.
I have already been able to generate a successful parser interpenetrated a line that is entered on the keyboard.
Parser example when I enter the keyboard "First Name: William", I managed to display on the screen the name of the variable and the value.
Now I have a file .txt who contain a large number of names and their value, and I would like to successfully display them on the screen.
below is my .jj file that I have already written to generate a parser of a typed line
Now i want the same but for a file.
options
{
static = true;
}
PARSER_BEGIN(parser_name)
public class parser_name
{
public static void main(String args []) throws ParseException
{
System.out.println("Waiting for the Input:");
parser_name parser = new parser_name(System.in);
parser.Start();
}
}
PARSER_END(parser_name)
SKIP :
{
" "
| "\r"
| "\t"
| "\n"
}
TOKEN : { < DIGIT : (["0"-"9"])+ > }
TOKEN : { <VARIABLE: (["a"-"z", "A"-"Z"])+> }
TOKEN : { <VALUE: (~["\n",":"])+> }
TOKEN : { <ASSIGNMENT: ":"> }
void Start(): { Token t,t1,t2;}
{
t=<VARIABLE>
t1=<ASSIGNMENT>
t2=<VALUE>
{ System.out.println("The Variable is "+t.image+",and the Value is "+t2.image); }
}
I have already tried to replace the "System.in" at the parser constructor with an object of type File.And then read the file by line, but it did not work.
Pass a Reader to the parser's constructor.

Semi-colon required, optional, or disallowed in gRPC option value?

I'm seeing one piece of code like the following:
rpc SayFallback (FooRequest) returns (FooResponse) {
option (com.example.proto.options.bar) = {
value : "{ message:\"baz\" }";
};
}
and another like the following:
rpc SayFallback (FooRequest) returns (FooResponse) {
option (com.example.proto.options.bar) = {
value : "{ message:\"baz\" }"
};
}
The first has a ; on the line with value while the second doesn't. Are either OK according to the standard?
Yes, they are considered optional. See the protobuf file source snippet:
while (!TryConsumeEndOfDeclaration("}", NULL)) {
if (AtEnd()) {
AddError("Reached end of input in method options (missing '}').");
return false;
}
if (TryConsumeEndOfDeclaration(";", NULL)) {
// empty statement; ignore
} else {
...
}

Get URL from nsIWebProgress STATE_REDIRECTING

I am looking for a simple solution to catch any window re-directions (301, 302 etc.) to retrieve the corresponding url from a nsiWebProgress.
Current solution
Right now, I am using the nsIWebProgress.NOTIFY_STATE_DOCUMENT event listener, processing any STATE_REDIRECTING status responses (also known as "flags") and iterating the responseHeaders from the corresponding request's nsIHttpChannel interface , as shown below.
Question
However, I am not sure, if I am using the best event listener (NOTIFY_STATE_DOCUMENT) and if this is really the fastest possible solution.
Furthermore, I am wondering, if firefox has processed any validation of the response's location header at this state, since I am tending to parse the location header by using the sdk/url.URL method, to be sure that the given location header is valid and no xss scam.
Client request:
GET /garage HTTP/1.1
Host: www.batmans.cave
Server response:
HTTP/1.1 301 Moved Permanently
Location: https://batmans.cave/garage/batmobile
Working example (simplified):
const { Ci } = require("chrome");
const LOCATION_HEADER = "location";
...
interfaces: ["nsIWebProgressListener", "nsISupportsWeakReference"],
add(browser) {
try {
browser.addProgressListener(this, Ci.nsIWebProgress.NOTIFY_STATE_DOCUMENT);
} catch (error) {}
},
...
/**
* Called when the state has changed for the window being watched changes.
* #see https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Reference/Interface/nsIWebProgressListener#onStateChange%28%29
* #param {nsIWebProgress} webProgress
* #param {nsIRequest} request
* #param {nsresult} status
* #param {String} message
*/
onStateChange(webProgress, request, status, message) {
if (status & Ci.nsIWebProgressListener.STATE_REDIRECTING) {
const window = webProgress.DOMWindow;
if (request instanceof Ci.nsIHttpChannel) {
const httpChannel = request.QueryInterface(Ci.nsIHttpChannel);
let location;
httpChannel.visitResponseHeaders(function (header, value) {
if (header.toLowerCase() == LOCATION_HEADER) {
location = value;
}
});
if (location) {
try {
const url = URL(location).toString();
console.log("redirect to", url); // OK! "https://batmans.cave/garage/batmobile"
}
catch (error) {}
}
}
}
}

JavaCC IntegerLiteral

I am using JavaCC to build a lexer and a parser and I have the following code:
TOKEN:
{
< #DIGIT : [ "0"-"9" ] >
|< INTEGER_LITERAL : (<DIGIT>)+ >
}
SimpleNode IntegerLiteral() :
{
Token t;
}
{
(t=<INTEGER_LITERAL>)
{
Integer n = new Integer(t.image);
jjtThis.jjtSetValue( n );
return jjtThis;
}
}
Hence it should accept only integers but it is also accepting 4. or 4 %%%%%% etc.
Try turn on debugging in your parser spec file like:
OPTIONS {
DEBUG_TOKEN_MANAGER=true
}
This will create a printout of what the TokenManager is doing while parsing.
"4." and "4%%%%" are not really accepted because what is read by your parser is always "4"
if you set you DEBUG_PARSER = true; in the OPTION section you will see the currently read token.
I think if you change your grammar like this you can see that it throws a TokenMgrError when it reads the unhandled character
SimpleNode IntegerLiteral() :
{
Token t;
}
{
(
t=<DIGIT>
{
Integer n = new Integer(t.image);
jjtThis.jjtSetValue( n );
return jjtThis;
})+
}

System.Web.HttpRequest - What is the difference between ContentLength, TotalBytes, and InputStream.Length?

I'm looking for answers to questions like these:
For any given request, do these three properties always return the same value?
Do any of them have side effects?
Do any of them block until the entire request is received by IIS?
Do any of them cause uploaded files to be loaded completely into memory?
I care about this because I'm making my web application email me when a request takes too long to process on the server, and I want to avoid sending this email for large requests, i.e. when the user uploads one or more large files.
According to MSDN:
ContentLength - specifies the length, in bytes, of content sent by the client.
TotalBytes - the number of bytes in the current input stream.
InputStream.Length - length of bytes in the input stream.
So last two are the same. Here is what Reflector says about ContentLength property:
public int ContentLength
{
get
{
if ((this._contentLength == -1) && (this._wr != null))
{
string knownRequestHeader = this._wr.GetKnownRequestHeader(11);
if (knownRequestHeader != null)
{
try
{
this._contentLength = int.Parse(knownRequestHeader, CultureInfo.InvariantCulture);
}
catch
{
}
}
else if (this._wr.IsEntireEntityBodyIsPreloaded())
{
byte[] preloadedEntityBody = this._wr.GetPreloadedEntityBody();
if (preloadedEntityBody != null)
{
this._contentLength = preloadedEntityBody.Length;
}
}
}
if (this._contentLength < 0)
{
return 0;
}
return this._contentLength;
}
}

Resources