-
-
Notifications
You must be signed in to change notification settings - Fork 172
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jquery ajax post doesn't work #223
Comments
This issue is jQuery related / Spring related. What happens:
|
Hi twendelmuth, i create right now and new issue on spring test github |
Okay that’s not what I meant The issue is that your code expects query parameters but a POST like your a sending it with jQuery will not contain those. In the end the call fails because of validation. |
Ok twendelmuth, can you suggest what can i change to work with HtmlUnit ? |
So you do have a different setup that you're actually testing than the one you provided in the test project? I don't think that the code you provided works in a real browser 👼 The easiest way to fix it would be to change the url in the
|
twendelmuth, it works great on chrome. you can test it ;) That the reason why i think there is a difference between a real browser and the MockMvc but spring test team dont think |
Okay I finally found some time to look into this. This one takes the request body without any escaping and passes it to the controller:
To get to the results I've transformed the project to use SpringBoot since it was easier for me to find out what's actually happening and where the error potentially is. I hope that it's running with gradle ... since I needed to convert it to a maven project because of my gradle issue 👼 The project is here: https://github.com/twendelmuth/spring-test-htmlunit I hope this helps :-) |
@guisimon28 as workaround we are using request rewriting in AjaxController, which reads queryparams from requestbody and sets it to requestparams list. |
hi vkandrotas, can you explain this workaround because it's not working in my case ? |
Hi, I work with @guisimon28. @twendelmuth, by testing the post request with html form POST submission ("FORM") and by ajax POST submission ("AJAX") I see this in So it seems that we could do something in Unfortunately, I know nothing of spring-test or HtmlUnit architecture... |
@fredarene not sure i got your point but from my point of view we should use the behavior of real browsers as reference. If HtmlUnit sends different requests its a bug. |
@rbri the issue is on the Spring Testing side from my pov. I've seen that the spring issue is open again & provided my answer from here there as well. |
@rbri if I understand correctly the requestParameters property of However when the form is posted like this: function postTest() {
$.ajax({
type:"post",
data: {
'subject' : 'subject test',
'message' : 'message test'
},
url:"[[@{/test/post}]]",
success: function(data, textStatus, jqXHR){
$("#result").text(data);
}
});
} it ends up in the request body which seems inconsistent since both are POST "application/x-www-form-urlencoded" requests with form data as content but one comes through with The reason it works when running with Tomcat is because Servlet containers parse the request body and populate I just want to double check is this intentional or a possible issue? |
From this i think the current implementation in HtmlUnit might be correct - but as always i'm not 100% sure. It will be great if you can run your sample
If there is a difference in the way the data are sent it will be helpful to strip down the jQuery sample to a plain javascript sample code. |
Any news here? |
still waiting an update from spring-test |
@guisimon28 what needs to happen is to answer the questions above. I haven't been able to get to it but anyone can do that. |
I don't think the problem is about a behavior difference with browsers, as the request seems to be sent correctly. Spring Framework, for its
The following repro project shows the difference of behavior: https://github.com/bclozel/htmlunitrepro We'd like to get the opinion from the HtmlUnit team about this. Thanks! |
Many thanks for the details, will have a look at this. |
Looks like this inconsistency makes no real sense. I like to write some HtmlUnit internal test for the various cases. |
@bclozel, @rstoyanchev, @guisimon28, @twendelmuth Hi all, have done some debugging. My findings so far:
For the sample code (jQuery) the call itself is already not symetric.
Still not sure how to proceed. |
This issue is not about making HtmlUnit consistent with the Servlet API or Servlet container behavior. The repro project is indeed using different HTTP calls and they're not equivalent. I guess we're asking the HtmlUnit team to clarify the meaning of
At the Spring level, we'll adapt to the team's decision. |
You are right, WebRequest#getRequestParameters or even the whole WebRequest class requires clarification. At the moment this is the (internal) way to construct a web request. From this point of view the interesting part are the setter methods. You can call setRequestParameter or setBody. There is some protection to make sure you are not overwriting data. Later on the internal machine is able to construct a valid Http request out of this. The machine uses the getter methods to determine what to do. Or to sum it up, there is some convenience in using the setters but not in the getters. The thinking behind the interface is more like a bean - getRequestParametes returns the value you have set before using setRequestParameter. This has nothing to do with the way the parameters are later encoded in the Http request. I think the major difference here is the different point of thinking. The WebRequest in HtmlUnit is the client point of view. As a client i like to create a WebRequest - usually i only like to say something like this: create a web request using this method and this parameters / this body. Your point of view is more like a server - you are called by the request and like to get the details out of the request. This is more or less the same work done by the translation process from the WebRequest into an HttpRequest. Will try to improve the javadoc also during the next days. If you like to have a decision: |
And by the way PR's are always welcome. |
Hi Rbri, how can we help you ? |
Hi Rbri, some developpers on my team can help you to find a fix. So how can we help you ? |
Hi @guisimon28
So far my understanding was that i do not have to change it in HtmlUnit and the fix will by done in spring. |
Happy new year everyone :-) I have a similar issue with htmlunit wenn posting a FormData object to a MockMvc Client.
see jquery docs Therefore the content of the "data" attribute in the But we have to accept that, the url in the ajax call could still contain other url params. I think that is, way the jquery docs state that in case of "GET" the "data" contant is APPENDED to the URL. So in htmlunit (in my opinion in Also we need to determine the enctype to send, as there is I'll try to have a look into this today but I'm not sure how far I'll get. Just in case it's not already known to the readers of this:
But that is on the server side, htmlunit and it's MockMvc "bridge" |
added another "default" case into the prepareRequestContext method for POST, PATH and PUT requests. If no other condition holds and before putting the content into the request body we now check if the encoding of the request is "application/x-www-form-urlencoded", parse the content and put the values into the request parameters. Those request parameters are later used by Springs RequestParamMethodArgumentResolver if htmlunit is running with MockMvc under the hood. This is necessary as there is no parsing of the body involved in Spring MockMvc. The implementation now behaves as it would if the javascript code passed a FormData instance as content. This should also work when htmlunit runs on apaches http client as HttpWebConnection will use the requestParameters for POST requests and recreate the urlencoded form body. The HttpWebConnection was also changed to do this for PATCH and PUT also if the encoding is application/x-www-form-urlencoded WIP: as I wasn't able to run all the tests yet
After looking deeper into this I changed my mind a bit. The fork is still work in progress as I wasn't sure which tests to run and which tests to update. |
@thuri can you please add a separate issue for your case; if you like we can have a talk about it later on today. Maybe you can suggest a timeslot (my email is in the pom). |
Hi @rbri I'll send you a mail but just for all others: There is no issue with posting FormData objects via mockMvc. |
Hi @thuri and happy new year every one. I've tried using HtmlUnit sources with your correction ( JQueryAJAXFormURLEncoded branch ), and on my side my issue is corrected. Now I can send a POST form request with Ajax ( and without my Spring PR ) If finally the correction must be done in HtmlUnit, I think I will not try to develop one as yours is correct for my needs, and I will wait that you do a PR, so thank you :) |
Hi,
I had a nice little chat with @rbri yesterday. He'll have a deeper look into this and come back to me with some help. I'll create a PR then if still necessary but probably it won't be necessary anymore
Am 5. Januar 2022 14:50:15 MEZ schrieb jcaillabet ***@***.***>:
…Hi @thuri and happy new year every one.
I've tried using HtmlUnit sources with your correction ( JQueryAJAXFormURLEncoded branch ), and on my side my issue is corrected. Now I can send a POST form request with Ajax ( and without my Spring PR )
If finally the correction must be done in HtmlUnit, I think I will not try to develop one as yours is correct for my needs, and I will wait that you do a PR, so thank you :)
--
Reply to this email directly or view it on GitHub:
#223 (comment)
You are receiving this because you were mentioned.
Message ID: ***@***.***>
|
Again two months later... Sorry |
…ameters implement WebRequest.getRequestParameters from scratch to take care of the different ways parameters are packed into requests This break the backward compatibility in some way but solves #223 and spring-projects/spring-framework#25768 (hopefully)
Hi @rbri , glad you found the time to look into this. BUT it looks like sending the data application/x-www-form-urlencoded using the GET method is broken now. I didn't had the time to look into the implementation but it seems like the value of the parameter is added twice to RequestParam parameters in the controller methods. |
@thuri thanks for testing, from my point of view i have nothing changed in the processing of xmlhttp requests. will have a look at your finding - i really like to get this closed ;-) |
ok, think i found it - but it looks like spring needs something special here.... |
Have created an Spring issue because i think it makes no sense to solve this in HtmlUnit only. |
…tUrlParameters and introduce WebRequest.getParameters() #223
As spring-projects/spring-framework#28240 is closed i will close this also. |
Hello,
i am using htmlunit with spring test in order to test all ihm interface from my web application. It works fine with html form (post and get) and with ajax get but i have a problem with ajax post request.
The controller don't received the request. If i replace post by get the junit test case works fine.
this the html view
and the controller
you can also find the complete code on my github https://github.com/guisimon28/spring-test-htmlunit
Can you help me to find if there is some missing configuration or if its a htmlunig bug or pull request ?
Thanks for All
Guillaume
The text was updated successfully, but these errors were encountered: