MockHttpRequest class
Mock implementation of the HttpRequest object returned from the HttpBackend.
class MockHttpRequest implements HttpRequest { final bool supportsCrossOrigin = false; final bool supportsLoadEndEvent = false; final bool supportsOverrideMimeType = false; final bool supportsProgressEvent = false; final Events on = null; final Stream<ProgressEvent> onAbort = null; final Stream<ProgressEvent> onError = null; final Stream<ProgressEvent> onLoad = null; final Stream<ProgressEvent> onLoadEnd = null; final Stream<ProgressEvent> onLoadStart = null; final Stream<ProgressEvent> onProgress = null; final Stream<ProgressEvent> onReadyStateChange = null; final Stream<ProgressEvent> onTimeout = null; final int readyState = 0; get responseText => response == null ? null : "$response"; final responseXml = null; final String statusText = null; final HttpRequestUpload upload = null; String responseType = null; int timeout = 0; bool withCredentials; final int status; final response; final String headers; MockHttpRequest(int this.status, String this.response, [this.headers]); void abort() {} bool dispatchEvent(Event event) => false; String getAllResponseHeaders() { if (headers == null) return null; return headers; } String getResponseHeader(String header) => null; void open(String method, String url, {bool async, String user, String password}) {} void overrideMimeType(String override) {} void send([data]) {} void setRequestHeader(String header, String value) {} void $dom_addEventListener(String type, EventListener listener, [bool useCapture]) {} void $dom_removeEventListener(String type, EventListener listener, [bool useCapture]) {} }
Implements
Constructors
new MockHttpRequest(int status, String response, [String headers]) #
General constructor for any type of request (GET, POST, etc).
This call is used in conjunction with open:
var request = new HttpRequest();
request.open('GET', 'http://dartlang.org');
request.onLoad.listen((event) => print(
'Request complete ${event.target.reponseText}'));
request.send();
is the (more verbose) equivalent of
HttpRequest.getString('http://dartlang.org').then(
(result) => print('Request complete: $result'));
MockHttpRequest(int this.status, String this.response, [this.headers]);
Properties
final int hashCode #
Get a hash code for this object.
All objects have hash codes. Hash codes are guaranteed to be the
same for objects that are equal when compared using the equality
operator ==
. Other than that there are no guarantees about
the hash codes. They will not be consistent between runs and
there are no distribution guarantees.
If a subclass overrides hashCode it should override the equality operator as well to maintain consistency.
int get hashCode => Primitives.objectHashCode(this);
final String headers #
final String headers
final Events on #
This is an ease-of-use accessor for event streams which should only be used when an explicit accessor is not available.
final Events on = null
final Stream<ProgressEvent> onAbort #
final Stream<ProgressEvent> onAbort = null
final Stream<ProgressEvent> onError #
final Stream<ProgressEvent> onError = null
final Stream<ProgressEvent> onLoad #
final Stream<ProgressEvent> onLoad = null
final Stream<ProgressEvent> onLoadEnd #
final Stream<ProgressEvent> onLoadEnd = null
final Stream<ProgressEvent> onLoadStart #
final Stream<ProgressEvent> onLoadStart = null
final Stream<ProgressEvent> onProgress #
final Stream<ProgressEvent> onProgress = null
final Stream<ProgressEvent> onReadyStateChange #
Event listeners to be notified every time the HttpRequest
object's readyState
changes values.
final Stream<ProgressEvent> onReadyStateChange = null
final Stream<ProgressEvent> onTimeout #
final Stream<ProgressEvent> onTimeout = null
final int readyState #
Indicator of the current state of the request:
Value | State | Meaning |
0 | unsent | open() has not yet been called |
1 | opened | send() has not yet been called |
2 | headers received | sent() has been called; response headers and status are available |
3 | loading | responseText holds some data |
4 | done | request is complete |
final int readyState = 0
final response #
final String responseText #
The response in string form or `null on failure.
final String responseText
String responseType #
String telling the server the desired response format.
Default is String
.
Other options are one of 'arraybuffer', 'blob', 'document', 'json',
'text'. Some newer browsers will throw NSERRORDOMINVALIDACCESS_ERR if
responseType
is set while performing a synchronous request.
See also: MDN responseType
String responseType = null
final responseXml #
The request response, or null on failure.
The response is processed as
text/xml
stream, unless responseType = 'document' and the request is
synchronous.
final responseXml = null
final Type runtimeType #
A representation of the runtime type of the object.
Type get runtimeType => getRuntimeType(this);
final int status #
The http result code from the request (200, 404, etc). See also: Http Status Codes
final int status
final String statusText #
The request response string (such as \"200 OK\"). See also: Http Status Codes
final String statusText = null
final bool supportsCrossOrigin #
Checks to see if the current platform supports making cross origin requests.
Note that even if cross origin requests are supported, they still may fail if the destination server does not support CORS requests.
final bool supportsCrossOrigin = false
final bool supportsLoadEndEvent #
Checks to see if the LoadEnd event is supported on the current platform.
final bool supportsLoadEndEvent = false
final bool supportsOverrideMimeType #
Checks to see if the overrideMimeType method is supported on the current platform.
final bool supportsOverrideMimeType = false
final bool supportsProgressEvent #
Checks to see if the Progress event is supported on the current platform.
final bool supportsProgressEvent = false
int timeout #
int timeout = 0
final HttpRequestUpload upload #
EventTarget that can hold listeners to track the progress of the request.
The events fired will be members of HttpRequestUploadEvents
.
final HttpRequestUpload upload = null
bool withCredentials #
True if cross-site requests should use credentials such as cookies or authorization headers; false otherwise.
This value is ignored for same-site requests.
bool withCredentials
Operators
bool operator ==(other) #
The equality operator.
The default behavior for all Objects is to return true if and
only if this
and
other are the same object.
Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:
-
Total: It must return a boolean for all arguments. It should never throw or return
null
. -
Reflexive: For all objects
o
,o == o
must be true. -
Symmetric: For all objects
o1
ando2
,o1 == o2
ando2 == o1
must either both be true, or both be false. -
Transitive: For all objects
o1
,o2
, ando3
, ifo1 == o2
ando2 == o3
are true, theno1 == o3
must be true.
The method should also be consistent over time, so equality of two objects should not change over time, or at least only change if one of the objects was modified.
If a subclass overrides the equality operator it should override the hashCode method as well to maintain consistency.
bool operator ==(other) => identical(this, other);
Methods
void abort() #
Stop the current request.
The request can only be stopped if readyState is HEADERS_RECIEVED
or
LOADING
. If this method is not in the process of being sent, the method
has no effect.
void abort() {}
bool dispatchEvent(Event event) #
bool dispatchEvent(Event event) => false;
String getAllResponseHeaders() #
Retrieve all the response headers from a request.
null
if no headers have been received. For multipart requests,
getAllResponseHeaders
will return the response headers for the current
part of the request.
See also HTTP response headers for a list of common response headers.
String getAllResponseHeaders() { if (headers == null) return null; return headers; }
String getResponseHeader(String header) #
Return the response header named header
, or null
if not found.
See also HTTP response headers for a list of common response headers.
String getResponseHeader(String header) => null;
dynamic noSuchMethod(Invocation invocation) #
noSuchMethod is invoked when users invoke a non-existant method on an object. The name of the method and the arguments of the invocation are passed to noSuchMethod in an Invocation. If noSuchMethod returns a value, that value becomes the result of the original invocation.
The default behavior of noSuchMethod is to throw a
noSuchMethodError
.
dynamic noSuchMethod(Invocation invocation) { throw new NoSuchMethodError( this, invocation.memberName, invocation.positionalArguments, invocation.namedArguments); }
void open(String method, String url, {bool async, String user, String password}) #
Specify the desired url
, and method
to use in making the request.
By default the request is done asyncronously, with no user or password
authentication information. If async
is false, the request will be send
synchronously.
Calling open
again on a currently active request is equivalent to
calling abort
.
Note: Most simple HTTP requests can be accomplished using the getString
,
request
, requestCrossOrigin
, or postFormData
methods. Use of this
open
method is intended only for more complext HTTP requests where
finer-grained control is needed.
void open(String method, String url, {bool async, String user, String password}) {}
void overrideMimeType(String override) #
Specify a particular MIME type (such as text/xml
) desired for the
response.
This value must be set before the request has been sent. See also the list of common MIME types
void overrideMimeType(String override) {}
void send([data]) #
Send the request with any given data
.
Note: Most simple HTTP requests can be accomplished using the getString
,
request
, requestCrossOrigin
, or postFormData
methods. Use of this
send
method is intended only for more complext HTTP requests where
finer-grained control is needed.
See also:
-
send from MDN.
void send([data]) {}
void setRequestHeader(String header, String value) #
void setRequestHeader(String header, String value) {}
String toString() #
Returns a string representation of this object.
String toString() => Primitives.objectToString(this);
void $dom_addEventListener(String type, EventListener listener, [bool useCapture]) #
void $dom_addEventListener(String type, EventListener listener, [bool useCapture]) {}
void $dom_removeEventListener(String type, EventListener listener, [bool useCapture]) #
void $dom_removeEventListener(String type, EventListener listener, [bool useCapture]) {}