I’m writing tests for my Grails-Application and I found a “bug” in my application – at least I think its my application.
My request looks like this:
http://maps.googleapis.com/maps/api/geocode/json?address=Karlsruhe+76131+Tullastraße+17&sensor=false
And the response contains my state / province (I don’t know the exact translation – I’m from Germany and it’s divided into 16 smaller “states” – called “Bundesland”)
My state is called “Baden-Württemberg” and thats in the response from the browser.
In my application I’m using an URL and a BufferedReader (with an InputStreamReader) to get the response.
I’m trying to assert, that the response from the browser is equal to the response I get from the URL-Connection in my code.
But – here comes the question/ problem – they differ. And the only difference I found was in the name of my state.
As mentioned before the response I got from the browser contains “Baden-Württemberg”, but the response I get through my code contains only “Baden-Wurttemberg” – the ü is switched with a regular u
Here’s my code:
URLConnection connection = new URL(url).openConnection()
BufferedReader br = new BufferedReader(new InputStreamReader(connection.content, "UTF-8" ))
StringBuffer connectionResponse = new StringBuffer()
while (br.ready()) {
connectionResponse.append(br.readLine())
}
br.close()
return connectionResponse.toString()
My search led me to the option of passing a charset to the InputStreamReader – as I expect it’s UTF-8 I use this charset, but it’s not solving this bug.
Im just confused why this is possible.
Where should I look ? I’m not that experienced so I’m rather clueless
Edit:
I tested some more and if I search with a street containing an umlaut (“Bärenweg” or “Baerenweg” would be an example), both responses (code and browser) contain the correct value: Bärenweg. It seems only the state is affected by this.
Should I contact google right away? With this new test I’m not really sure if I can do something about it
The problem is solved.
I asked a colleague of mine and he suggested, that the only reasonable cause could be different Request-Headers.
The only way I could think of right away was to do it with wireshark. So i downloaded and installed it (WinPCab installed with it) and startet it after the installation.
First I triggered both requests by pressing F5 in Firefox and running my tests in Grails
Then I added the filter “http.response” (meaning all responses from HTTP request should be visible).
The Response-Headers were almost equal and it didn’t occur any obvious mistake.
Then I filtered for “http.request” to compare the headers sent to the google-api and there was a bit more off, then in the response.
First I compared the Accept-Property and set the property in my code to the value from the Browser-Response (the one, which works fine).
Didn’t solve the problem.
So next property: Accept-Language.
In the Request-header from the code this property wasn’t there at all, so I copied it from the Browser-Request-header and kazaam: it works!
to set this property on the connection add the following code:
TL;DR / Long story short
set the language-property in the request-header and google knows what language you expect the response to be in.