¿Cómo puedo garantizar que la petición se realizó de forma correcta cuando se burla de una API?

0

Pregunta

Digamos que estoy probando una característica que llama a un servicio web, y que el servicio se burlaban con httptest.NewServer

func TestSomeFeature(t *testing.T) {
    server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        w.WriteHeader(200)
    }))

    SomeFeature(server.URL, "foo")
}

func SomeFeature(host, a string) {
    if a == "foo" {
        http.Get(fmt.Sprintf("%v/foo", host))
    }
    if a == "bar" {
        http.Get(fmt.Sprintf("%v/bar", host))
    }
}

¿Cómo puedo afirmar que el servidor se ha llamado a la url correcta /foo y fallar la prueba si es llamada con la dirección url incorrecta o no llamado a todos?

go testing
2021-11-23 21:15:23
1

Mejor respuesta

3

Usted puede hacer esto de esta manera:

func TestSomeFeature(t *testing.T) {
    called := false
    server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
        // assert that strings.Contains(r.RequestURI, "/foo") is true
        called = true
        w.WriteHeader(200)
    }))

    SomeFeature(server.URL, "foo")
    // assert that called is true
}
2021-11-23 21:19:38

No hay problemas de concurrencia si me ejecución de varios de estos en paralelo con el mismo server?
Timo Huovinen

@TimoHuovinen por supuesto el código de arriba es para ensayos secuenciales, no especificó la concurrencia en su pregunta
blackgreen

En otros idiomas

Esta página está en otros idiomas

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Slovenský
..................................................................................................................