2020-07-04 10:09:35 +03:00
package requests
import (
"encoding/json"
"fmt"
2022-10-21 13:57:51 +03:00
"io"
2020-07-04 10:09:35 +03:00
"log"
"net/http"
"net/http/httptest"
"testing"
2020-09-29 19:44:42 +03:00
"github.com/oauth2-proxy/oauth2-proxy/v7/pkg/logger"
2024-07-18 23:41:02 +03:00
. "github.com/onsi/ginkgo/v2"
2020-07-04 10:09:35 +03:00
. "github.com/onsi/gomega"
)
var (
server * httptest . Server
serverAddr string
)
func TestRequetsSuite ( t * testing . T ) {
logger . SetOutput ( GinkgoWriter )
2021-02-10 21:50:09 +03:00
logger . SetErrOutput ( GinkgoWriter )
2020-07-04 10:09:35 +03:00
log . SetOutput ( GinkgoWriter )
RegisterFailHandler ( Fail )
RunSpecs ( t , "Requests Suite" )
}
var _ = BeforeSuite ( func ( ) {
// Set up a webserver that reflects requests
mux := http . NewServeMux ( )
mux . Handle ( "/json/" , & testHTTPUpstream { } )
mux . HandleFunc ( "/string/" , func ( rw http . ResponseWriter , _ * http . Request ) {
rw . Write ( [ ] byte ( "OK" ) )
} )
server = httptest . NewServer ( mux )
serverAddr = fmt . Sprintf ( "http://%s" , server . Listener . Addr ( ) . String ( ) )
} )
var _ = AfterSuite ( func ( ) {
server . Close ( )
} )
// testHTTPRequest is a struct used to capture the state of a request made to
// the test server
type testHTTPRequest struct {
Method string
Header http . Header
Body [ ] byte
RequestURI string
}
type testHTTPUpstream struct { }
func ( t * testHTTPUpstream ) ServeHTTP ( rw http . ResponseWriter , req * http . Request ) {
request , err := toTestHTTPRequest ( req )
if err != nil {
t . writeError ( rw , err )
return
}
data , err := json . Marshal ( request )
if err != nil {
t . writeError ( rw , err )
return
}
rw . Header ( ) . Set ( "Content-Type" , "application/json" )
rw . Write ( data )
}
func ( t * testHTTPUpstream ) writeError ( rw http . ResponseWriter , err error ) {
rw . WriteHeader ( 500 )
if err != nil {
rw . Write ( [ ] byte ( err . Error ( ) ) )
}
}
func toTestHTTPRequest ( req * http . Request ) ( testHTTPRequest , error ) {
requestBody := [ ] byte { }
if req . Body != http . NoBody {
var err error
2022-10-21 13:57:51 +03:00
requestBody , err = io . ReadAll ( req . Body )
2020-07-04 10:09:35 +03:00
if err != nil {
return testHTTPRequest { } , err
}
}
return testHTTPRequest {
Method : req . Method ,
Header : req . Header ,
Body : requestBody ,
RequestURI : req . RequestURI ,
} , nil
}