ResponseObject#getHeaderFields() returns Map with key of null value

## Operating System: Windows 7

## Katalon Studio Version: 5.8.0

## Katalon Studio logs:

  • attached

## Environment (for Web testing)

  • Firefox 62.0.3

## Steps to reproduce

I created a test case :

import static com.kms.katalon.core.testobject.ObjectRepository.findTestObject
import java.nio.file.Files
import java.nio.file.Path
import java.nio.file.Paths
import com.kms.katalon.core.testobject.ResponseObject
import com.kms.katalon.core.webservice.keyword.WSBuiltInKeywords as WS
import groovy.json.JsonOutput
Path tmpDir = Paths.get("./tmp")
Files.createDirectories(tmpDir)
// Send the request and get the response
ResponseObject response = WS.sendRequest(
findTestObject('Simple examples/api-2-issue/Get issue/Get an issue by Key'))
// Verify the response
WS.verifyResponseStatusCode(response, 200)
WS.verifyElementPropertyValue(response, 'fields.project.key', 'KD')
// obtain Response HEADER fields
Map<String, List<String>> headerFields = response.getHeaderFields()
// store the HTTP Response Headers into file as plain text
Path path1 = tmpDir.resolve("headers.txt")
path1.toFile().append(headerFields.toString(), 'utf-8')
// try to convert the Map into a string in Json format
def jsonStr = JsonOutput.toJson(headerFields)
Path path2 = tmpDir.resolve("headers.json")
path2.toFile().append(JsonOutput.prettyPrint(jsonStr), 'utf-8')

When executed I got the following error message:

Test Cases/TC1 FAILED because (of) (Stack trace: java.lang.IllegalArgumentException:     Maps with null keys can't be converted to JSON
at groovy.json.JsonOutput.writeMap(JsonOutput.java:448)
at groovy.json.JsonOutput.toJson(JsonOutput.java:217)
at groovy.json.JsonOutput$toJson$0.call(Unknown Source)
at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:48)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:113)
at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125)
at Script1539835810685.run(Script1539835810685.groovy:31)

The file “header.txt” looks as follows

{Transfer-Encoding=[chunked], X-AREQUESTID=[2c853182-f42c-4206-9e1c-fbdb7a6daaad],   null=[HTTP/1.1 200],   Server=[Atlassian Proxy/1.13.6.2], ATL-TraceId=[e418911fb6031799], X-Content-Type-Options=[nosniff], Connection=[keep-alive], ATL-TCS-Time=[0], Date=[Thu, 18 Oct 2018 04:15:48 GMT], Strict-Transport-Security=[max-age=315360000; includeSubDomains; preload], Cache-Control=[no-cache, no-store, no-transform], X-AUSERNAME=[trongbui], Set-Cookie=[atlassian.xsrf.token=8cb37754-f6ef-4067-a369-e75edecd7308_368ae44ae104efa79e72c8e1545e2491d7766345_lin; Path=/; Secure], Vary=[Accept-Encoding], X-XSS-Protection=[1; mode=block], Content-Type=[application/json;charset=UTF-8]}{Transfer-Encoding=[chunked], X-AREQUESTID=[cd799583-7181-4da3-8016-7cd4b95e7af3], null=[HTTP/1.1 200], Server=[Atlassian Proxy/1.13.6.2], ATL-TraceId=[bbde75ad1167bf95], X-Content-Type-Options=[nosniff], Connection=[keep-alive], ATL-TCS-Time=[1], Date=[Thu, 18 Oct 2018 04:21:09 GMT], Strict-Transport-Security=[max-age=315360000; includeSubDomains; preload], Cache-Control=[no-cache, no-store, no-transform], X-AUSERNAME=[trongbui], Set-Cookie=[atlassian.xsrf.token=8cb37754-f6ef-4067-a369-e75edecd7308_8fc2d71a0fd804e7d614e8d45495d893bb9a7056_lin; Path=/; Secure], Vary=[Accept-Encoding], X-XSS-Protection=[1; mode=block], Content-Type=[application/json;charset=UTF-8]}

Obviously, the null key is causing problem.

**The TestObject **‘Simple examples/api-2-issue/Get issue/Get an issue by Key’ is just the same as

## Expected Behavior

The Map object is safely converted into Json string

## Actual Behavior

java.lang.IllegalArgumentException:     Maps with null keys can't be converted to JSON

**## Screenshots / Videos: **

**
**

.log

MapWithNullKeysCantBeConvertedToJson.png

1 Like

As a workaround, I wrote a custom keyword.

	@Keyword	String convertHeaderFieldsToJsonString(Map<String, List<String>> headerFields) {		Map<String, List<String>> filtered = new HashMap<String, List<String>>()		for (String key : headerFields.keySet()) {			List<String> value = headerFields.get(key)			if (key != null) {				filtered.put(key, value)			} else {				filtered.put('null', value)			}		}		return JsonOutput.toJson(filtered)	}

and used it as follows:

// store the HTTP Response Headers into filePath path2 = mr.resolveMaterialPath(GlobalVariable.CURRENT_TESTCASE_ID, "headers.json")Map<String, List<String>> headerFields = response.getHeaderFields()String headersJson = CustomKeywords.'com.kazurayam.ksbackyard.WebServiceTestSupport.convertHeaderFieldsToJsonString'(headerFields)path2.toFile().append(headersJson, 'utf-8')
1 Like