Package io.karatelabs.http
Class HttpResponse
java.lang.Object
io.karatelabs.http.HttpResponse
- All Implemented Interfaces:
io.karatelabs.js.ObjectLike
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic HttpResponsebadRequest(String message) Shortcut forerror(400, message).static HttpResponsebinary(byte[] body) 200 OK+application/octet-stream.static HttpResponseStatus + JSON body{"error": message}.getBody()byte[]intParse cookies from Set-Cookie headers using Netty's cookie decoder.intgetDelay()getHeaderValues(String name) io.karatelabs.common.ResourceTypelonglongintstatic HttpResponse200 OK+text/html.booleanReturns true if this response represents a skipped request.static HttpResponseStatus +application/json.static HttpResponse200 OK+application/json.static HttpResponse204 No Content.static HttpResponseShortcut forerror(404, message).static HttpResponseok()200 OKwith no body.static HttpResponse200 OKwith body type inferred (matches the JS-mock dispatch):String→text/plain,Map/List→application/json,Node→application/xml,byte[]→no Content-Type.voidstatic HttpResponseCustom redirect status (301 / 303 / 307 / 308) +Locationheader.static HttpResponse302 Found+Locationheader.voidremoveHeader(String name) Remove a header by name.voidremoveMember(String name) voidsetBody(byte[] body, io.karatelabs.common.ResourceType type) Canonical body setter.voidConvenience overload forsetBody(byte[], ResourceType)that UTF-8-encodes the supplied String.voidsetBodyDynamic(Object value) Type-dispatched body assignment for dynamic call sites — mock dispatch and the JS-mockresponse.body = ...path.voidsetContentLength(int contentLength) voidsetContentType(String contentType) voidsetDelay(int delay) voidvoidvoidsetHeaders(Map<String, ?> map) voidsetRequest(HttpRequest request) voidsetResourceType(io.karatelabs.common.ResourceType resourceType) voidsetResponseTime(long responseTime) voidsetStartTime(long startTime) voidsetStatus(int status) voidPaired status setter — sets status code and reason text atomically.voidsetStatusText(String statusText) static HttpResponseskipped(HttpRequest request) Creates a skipped response for when a listener returns false from HTTP_ENTER.static HttpResponseStatus +text/plain.static HttpResponse200 OK+text/plain.toMap()static HttpResponse200 OK+application/xml.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface io.karatelabs.js.ObjectLike
getMember, getPrototype, isExtensible, isFrozen, isOwnProperty, isSealed, putMember, removeMember, setExtensible, setFrozen, setSealed
-
Constructor Details
-
HttpResponse
public HttpResponse()
-
-
Method Details
-
getResourceType
public io.karatelabs.common.ResourceType getResourceType() -
getContentType
-
getHeader
-
getHeaderValues
-
getStatus
public int getStatus() -
setStatus
public void setStatus(int status) -
setStatus
Paired status setter — sets status code and reason text atomically. Equivalent tosetStatus(int)followed bysetStatusText(String). -
setContentLength
public void setContentLength(int contentLength) -
getContentLength
public int getContentLength() -
getStatusText
-
setStatusText
-
getHeaders
-
setContentType
-
setHeader
-
getRequest
-
setRequest
-
setStartTime
public void setStartTime(long startTime) -
getStartTime
public long getStartTime() -
setHeader
-
removeHeader
Remove a header by name. Case-insensitive match — same convention asgetHeader(String)/getHeaderValues(String). No-op if the header is absent or no headers have been set yet. -
setHeaders
-
getBody
-
getBodyBytes
public byte[] getBodyBytes() -
getBodyString
-
setBody
public void setBody(byte[] body, io.karatelabs.common.ResourceType type) Canonical body setter.typedrives the Content-Type header (passnullif the caller will set Content-Type explicitly). For text-based types (text/*,application/json|xml, etc.) the default UTF-8 charset is appended — seeapplyCharset(io.karatelabs.common.ResourceType). The implicit-type-inference overloads were removed in favour of the static factories (text(java.lang.String),json(java.lang.Object),html(java.lang.String), ...). -
setBody
Convenience overload forsetBody(byte[], ResourceType)that UTF-8-encodes the supplied String. Caller still passes the explicit type — no hidden defaults. -
getBodyConverted
-
getResponseTime
public long getResponseTime() -
setResponseTime
public void setResponseTime(long responseTime) -
setResourceType
public void setResourceType(io.karatelabs.common.ResourceType resourceType) -
getDelay
public int getDelay() -
setDelay
public void setDelay(int delay) -
getCookies
Parse cookies from Set-Cookie headers using Netty's cookie decoder. Returns a map where each cookie name maps to a map with properties like 'value', 'domain', 'path', etc. This matches V1 behavior where cookies are accessed as: responseCookies['name'].value -
toMap
- Specified by:
toMapin interfaceio.karatelabs.js.ObjectLike
-
skipped
Creates a skipped response for when a listener returns false from HTTP_ENTER. The status is set to 0 to indicate the request was not actually made. -
isSkipped
public boolean isSkipped()Returns true if this response represents a skipped request. A request is skipped when a listener returns false from HTTP_ENTER event. -
getMember
- Specified by:
getMemberin interfaceio.karatelabs.js.ObjectLike
-
putMember
- Specified by:
putMemberin interfaceio.karatelabs.js.ObjectLike
-
setBodyDynamic
Type-dispatched body assignment for dynamic call sites — mock dispatch and the JS-mockresponse.body = ...path. Map/List → JSON, Node → XML, String → TEXT, byte[] → null-type (caller controls Content-Type for raw bytes — historical behaviour for raw bytes). Production code that knows the body type should prefer one of the static factories (json(Object),text(String), etc.). -
ok
200 OKwith no body. -
ok
200 OKwith body type inferred (matches the JS-mock dispatch):String→text/plain,Map/List→application/json,Node→application/xml,byte[]→no Content-Type. -
text
200 OK+text/plain. -
text
Status +text/plain. -
json
200 OK+application/json. -
json
Status +application/json.Stringbodies are treated as already-encoded JSON; everything else goes throughJson.toBytes. -
html
200 OK+text/html. -
xml
200 OK+application/xml. -
binary
200 OK+application/octet-stream. -
noContent
204 No Content. -
redirect
302 Found+Locationheader. -
redirect
Custom redirect status (301 / 303 / 307 / 308) +Locationheader. -
error
Status + JSON body{"error": message}.nullmessage becomes"". -
badRequest
Shortcut forerror(400, message). -
notFound
Shortcut forerror(404, message). -
removeMember
- Specified by:
removeMemberin interfaceio.karatelabs.js.ObjectLike
-