### Question:
CodeRequest { public static CodeRequest newCodeRequest(HarRequest harRequest) throws Exception { return new CodeRequest(harRequest); } private  CodeRequest(HarRequest harRequest); Map<String, List<String>> queryStringsToMap(); Optional<HarHeader> find(String headerName); String getMethod(); String toJsonString(); String toPrettyJsonString(); Map<String, Object> fromJsonString(); String getHttpVersion(); List<HarCookie> getCookies(); List<HarHeader> getHeaders(); List<HarQueryString> getQueryStrings(); List<HarParam> getParams(); String getMimeType(); String getText(); boolean hasText(); boolean hasParams(); boolean hasHeaders(); boolean hasCookies(); boolean hasHeadersAndCookies(); boolean hasQueryStrings(); boolean hasAttachments(); boolean hasBody(); Map<String, String> headersAsMap(); Map<String, String> allHeadersAsMap(); Map<String, List<String>> queryStringsAsMap(); Map<String, String> paramsAsMap(); Map<String, String> cookiesAsMap(); String getCookieString(); String paramsToJSONString(); String paramsToPrettyJSONString(); String paramsToString(); Map<String, Object> unwrapQueryStrings(); String queryStringsToJsonString(); String headersToJsonString(boolean pretty); String headersToJsonString(); String allHeadersToJsonString(boolean pretty); String allHeadersToJsonString(); Map<String, String> newMap(Map<String, String> map); String getHost(); int getPort(); String getProtocol(); String getPath(); String getFullPath(); String getUrl(); String getFullUrl(); boolean isSecure(); static CodeRequest newCodeRequest(HarRequest harRequest); boolean isDefaultPort(); static final int HTTP_PORT; static final int HTTPS_PORT; }### Answer:
@Test void testExceptionRaisedWhenUrlIsMalformed() { HarRequest harRequest = new HarRequest().withUrl("localhost"); Exception thrown = assertThrows( Exception.class, () -> newCodeRequest(harRequest), "Expected exception thrown"); assertEquals("Malformed url", thrown.getMessage()); }
@Test void testExceptionIsRaisedWhenContentTypeIsJsonAndPostDataTextIsNotJson() { HarRequest harRequest = new HarRequest() .withUrl(HTTP_URL) .withPostData(new HarPostData().withMimeType(MediaType.APPLICATION_JSON)) ; Exception thrown = assertThrows( Exception.class, () -> newCodeRequest(harRequest), "Expected exception thrown"); assertEquals("JSON validation failed", thrown.getMessage()); }
@Test void testExceptionIsThrownWhenContentTypeIsMultiFormDataAndListOfParamsIsEmpty() { HarRequest harRequest = new HarRequest() .withUrl(HTTP_URL) .withPostData( new HarPostData(MediaType.MULTIPART_FORM_DATA, new ArrayList<>(), null, null)) ; Exception thrown = assertThrows( Exception.class, () -> newCodeRequest(harRequest), "Expected exception thrown"); assertEquals("Params cannot be empty", thrown.getMessage()); }
@Test void testExceptionIsRaisedWhenContentTypeIsFormUrlEncodedAndListOfParamsIsEmpty() { HarRequest harRequest = new HarRequest() .withUrl(HTTP_URL) .withPostData( new HarPostData( MediaType.APPLICATION_FORM_URLENCODED, new ArrayList<>(), null, null)) ; Exception thrown = assertThrows( Exception.class, () -> newCodeRequest(harRequest), "Expected exception thrown"); assertEquals("Params cannot be empty", thrown.getMessage()); }
@Test void testExceptionRaisedWhenRequestIsNull() { Exception thrown = assertThrows(Exception.class, () -> newCodeRequest(null), "Expected exception thrown"); assertEquals(thrown.getMessage(), "Har Request cannot be null"); }