Documentation in literate-programming-style is available at:
https://redhatinsights.github.io/insights-operator-ldapauth/packages/server/auth_test.html
|
import (
"bytes"
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
var testServer = & Server {
Address : ":8081" ,
LDAP : "" ,
Proxy : "http://localhost:8080" ,
Transport : http . DefaultTransport ,
}
func TestServerLoginUnauthorized ( t * testing . T ) {
body , _ := json . Marshal ( map [ string ] interface { } { "login" : "tester" , "password" : "1234" } )
req , err := http . NewRequest ( "POST" , APIPrefix + "login" , bytes . NewBuffer ( body ) )
if err != nil {
t . Errorf ( "Unexpected error %s" , err )
}
rr := httptest . NewRecorder ( )
handler := http . HandlerFunc ( testServer . Login )
handler . ServeHTTP ( rr , req )
|
Check content-type header
|
if contentType := rr . Header ( ) . Get ( "Content-Type" ) ; contentType != "application/json; charset=utf-8" {
t . Errorf ( "Expected Content-Type header value: %v but got %v" ,
"application/json; charset=utf-8" , contentType )
}
|
Check the status code
|
if status := rr . Code ; status != http . StatusUnauthorized {
t . Errorf ( "Expected status code: %v but got %v" ,
http . StatusUnauthorized , status )
}
}
func TestJwtAuthNoProxy ( t * testing . T ) {
body , _ := json . Marshal ( map [ string ] interface { } { "login" : "tester" , "password" : "1234" } )
req , err := http . NewRequest ( "POST" , APIPrefix + "login" , bytes . NewBuffer ( body ) )
if err != nil {
t . Errorf ( "Unexpected error %s" , err )
}
rr := httptest . NewRecorder ( )
handler := http . HandlerFunc ( testServer . HandleHTTP )
newHandler := testServer . JWTAuthentication ( handler )
newHandler . ServeHTTP ( rr , req )
|