samedi 30 avril 2016

Detecting and testing a non responsive server in angular

I have a http request similar to this:

const timeout = $q.defer()
let timedOut = false

setTimeout(() => {
  timedOut = true
  timeout.resolve()
}, 4000)

$http({
  method: 'GET',
  url: 'www.someurl.com',
  timeout: timeout.promise
}).then((response) => {
    return response.data
  })
  .catch(() => {
    if (timedOut) {
      return "server is not responding"
    } else {
      return "some other error"
    }
  })

The purpose of this code is to send an http request, and if after 4 seconds there is no response, the request is cancelled, returning a message that the server is unresponsive.

The problem is that I have no idea how I would test something like this, to see if it actually works. I normally use unit tests with $httpBackend to mock requests, but in this case it would not work. Is this the correct approach?

Aucun commentaire:

Enregistrer un commentaire