SimulatedResponse constructor

SimulatedResponse(
  1. _CoreResponseSimulated core,
  2. String sessionCookieName
)

Constructor

Creates a simulated response.

Implementation

SimulatedResponse(_CoreResponseSimulated core, String sessionCookieName) {
  ArgumentError.checkNotNull(core);
  ArgumentError.checkNotNull(sessionCookieName);

  _status = core.status;

  contentType = core.headers.contentType;

  // Copy cookies from core's cookies (omitting any session cookie)

  assert(sessionId == null);

  for (var c in core.cookies) {
    if (c.name == sessionCookieName) {
      // Session cookie: do not copy it to the cookies, but use as sessionId

      final _maxAge = c.maxAge;
      final _expires = c.expires;

      if (c.value.isNotEmpty &&
          (_maxAge == null || 0 < _maxAge) &&
          (_expires == null || 1970 < _expires.year)) {
        // Is setting the session cookie (not deleting it): set the session
        assert(sessionId == null);
        sessionId = c.value;
      } else {
        // Not the session cookie: copy it
        cookieAdd(c);
      }
    }
  }

  // Copy headers from core's headers

  core.headers.forEach((name, values) {
    for (var v in values) {
      headerAdd(name, v);
    }
  });

  // Set the body using one of (but not both) string or bytes

  if (core.hasBody) {
    if (core.hasBodyString) {
      _bodyStr = core.bodyStr();
      _body = SimulatedResponseBodyType.string;
    } else if (core.hasBodyBytes) {
      _bodyBytes = core.bodyBytes();
      _body = SimulatedResponseBodyType.bytes;
    } else {
      throw StateError('core has neither bytes or string body');
    }
  } else {
    _body = SimulatedResponseBodyType.none;
  }
}