Class HttpResponse

java.lang.Object
io.karatelabs.http.HttpResponse
All Implemented Interfaces:
io.karatelabs.js.ObjectLike

public class HttpResponse extends Object implements io.karatelabs.js.ObjectLike
  • Constructor Details

    • HttpResponse

      public HttpResponse()
  • Method Details

    • getResourceType

      public io.karatelabs.common.ResourceType getResourceType()
    • getContentType

      public String getContentType()
    • getHeader

      public String getHeader(String name)
    • getHeaderValues

      public List<String> getHeaderValues(String name)
    • getStatus

      public int getStatus()
    • setStatus

      public void setStatus(int status)
    • setStatus

      public void setStatus(int status, String statusText)
      Paired status setter — sets status code and reason text atomically. Equivalent to setStatus(int) followed by setStatusText(String).
    • setContentLength

      public void setContentLength(int contentLength)
    • getContentLength

      public int getContentLength()
    • getStatusText

      public String getStatusText()
    • setStatusText

      public void setStatusText(String statusText)
    • getHeaders

      public Map<String,List<String>> getHeaders()
    • setContentType

      public void setContentType(String contentType)
    • setHeader

      public void setHeader(String name, List<String> values)
    • getRequest

      public HttpRequest getRequest()
    • setRequest

      public void setRequest(HttpRequest request)
    • setStartTime

      public void setStartTime(long startTime)
    • getStartTime

      public long getStartTime()
    • setHeader

      public void setHeader(String name, String... values)
    • removeHeader

      public void removeHeader(String name)
      Remove a header by name. Case-insensitive match — same convention as getHeader(String) / getHeaderValues(String). No-op if the header is absent or no headers have been set yet.
    • setHeaders

      public void setHeaders(Map<String,?> map)
    • getBody

      public Object getBody()
    • getBodyBytes

      public byte[] getBodyBytes()
    • getBodyString

      public String getBodyString()
    • setBody

      public void setBody(byte[] body, io.karatelabs.common.ResourceType type)
      Canonical body setter. type drives the Content-Type header (pass null if the caller will set Content-Type explicitly). For text-based types (text/*, application/json|xml, etc.) the default UTF-8 charset is appended — see applyCharset(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

      public void setBody(String body, io.karatelabs.common.ResourceType type)
      Convenience overload for setBody(byte[], ResourceType) that UTF-8-encodes the supplied String. Caller still passes the explicit type — no hidden defaults.
    • getBodyConverted

      public Object 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

      public Map<String,Map<String,Object>> 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

      public Map<String,Object> toMap()
      Specified by:
      toMap in interface io.karatelabs.js.ObjectLike
    • skipped

      public static HttpResponse skipped(HttpRequest request)
      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

      public Object getMember(String key)
      Specified by:
      getMember in interface io.karatelabs.js.ObjectLike
    • putMember

      public void putMember(String key, Object value)
      Specified by:
      putMember in interface io.karatelabs.js.ObjectLike
    • setBodyDynamic

      public void setBodyDynamic(Object value)
      Type-dispatched body assignment for dynamic call sites — mock dispatch and the JS-mock response.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

      public static HttpResponse ok()
      200 OK with no body.
    • ok

      public static HttpResponse ok(Object body)
      200 OK with body type inferred (matches the JS-mock dispatch): String→text/plain, Map/List→application/json, Node→application/xml, byte[]→no Content-Type.
    • text

      public static HttpResponse text(String body)
      200 OK + text/plain.
    • text

      public static HttpResponse text(int status, String body)
      Status + text/plain.
    • json

      public static HttpResponse json(Object body)
      200 OK + application/json.
    • json

      public static HttpResponse json(int status, Object body)
      Status + application/json. String bodies are treated as already-encoded JSON; everything else goes through Json.toBytes.
    • html

      public static HttpResponse html(String body)
      200 OK + text/html.
    • xml

      public static HttpResponse xml(String body)
      200 OK + application/xml.
    • binary

      public static HttpResponse binary(byte[] body)
      200 OK + application/octet-stream.
    • noContent

      public static HttpResponse noContent()
      204 No Content.
    • redirect

      public static HttpResponse redirect(String location)
      302 Found + Location header.
    • redirect

      public static HttpResponse redirect(int status, String location)
      Custom redirect status (301 / 303 / 307 / 308) + Location header.
    • error

      public static HttpResponse error(int status, String message)
      Status + JSON body {"error": message}. null message becomes "".
    • badRequest

      public static HttpResponse badRequest(String message)
      Shortcut for error(400, message).
    • notFound

      public static HttpResponse notFound(String message)
      Shortcut for error(404, message).
    • removeMember

      public void removeMember(String name)
      Specified by:
      removeMember in interface io.karatelabs.js.ObjectLike