Skip to content
This repository
tree: d673f5cb79
Fetching contributors…

Cannot retrieve contributors at this time

file 57 lines (47 sloc) 1.164 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
package co.freeside.betamax.message.tape

import co.freeside.betamax.encoding.*
import co.freeside.betamax.message.*
import org.apache.http.HttpHeaders

abstract class RecordedMessage extends AbstractMessage implements Message {

Map<String, String> headers = [:]
def body

final void addHeader(String name, String value) {
if (headers[name]) {
headers[name] = "${headers[name]}, $value"
} else {
headers[name] = value
}
}

final boolean hasBody() {
body
}

@Override
final Reader getBodyAsText() {
String string
if (body) {
string = body instanceof String ? body : getEncoder().decode(bodyAsBinary, charset)
} else {
string = ''
}

new StringReader(string)
}

final InputStream getBodyAsBinary() {
byte [] bytes
if (body) {
bytes = body instanceof String ? getEncoder().encode(body, charset) : body
} else {
bytes = new byte [0]
}

new ByteArrayInputStream(bytes)
}

private AbstractEncoder getEncoder() {
switch (getHeader(HttpHeaders.CONTENT_ENCODING)) {
case 'gzip':
return new GzipEncoder()
case 'deflate':
return new DeflateEncoder()
default:
return new NoOpEncoder()
}
}
}
Something went wrong with that request. Please try again.