-
Notifications
You must be signed in to change notification settings - Fork 0
/
applicationserver.go
79 lines (60 loc) · 2.3 KB
/
applicationserver.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package mock
import (
"context"
"fmt"
"net/http"
log "github.com/sirupsen/logrus"
"github.com/gorilla/mux"
)
type AppMockServer struct {
*http.Server
port int32
}
func NewAppMockServer(port int32) *AppMockServer {
router := mux.NewRouter()
router.Path("/status/ok").HandlerFunc(StatusOK)
basicAuth := NewBasicAuthHandler()
router.Path("/auth/basic/{username}/{password}").HandlerFunc(basicAuth.BasicAuth)
router.Path("/spec/auth/basic/{username}/{password}").HandlerFunc(basicAuth.BasicAuthSpec)
oAuth := NewOauthHandler()
router.Path("/auth/oauth/token/{clientid}/{clientsecret}").HandlerFunc(oAuth.OAuthTokenHandler)
router.Path("/auth/oauth/token/{clientid}/{clientsecret}/headers/{header}/{value}").HandlerFunc(oAuth.OAuthTokenHeadersHandler)
router.Path("/auth/oauth/token/{clientid}/{clientsecret}/queryparams{param}/{value}").HandlerFunc(oAuth.OAuthTokenQueryParamsHandler)
router.Path("/spec/auth/oauth").HandlerFunc(oAuth.OAuthSpecHandler)
router.Path("/target/auth/oauth").HandlerFunc(oAuth.OAuthTargetHandler)
headers := NewHeadersHandler()
router.Path("/headers/{header}/{value}").HandlerFunc(headers.HeadersHandler)
router.Path("/spec/headers/{header}/{value}").HandlerFunc(headers.HeadersHandlerSpec)
queryParams := NewQueryParamsHandler()
router.Path("/queryparams/{param}/{value}").HandlerFunc(queryParams.QueryParamsHandler)
router.Path("/spec/queryparams/{param}/{value}").HandlerFunc(queryParams.QueryParamsHandlerSpec)
csrf := NewCsrfHandler()
router.Path("/csrftoken").HandlerFunc(csrf.CsrfToken)
router.Path("/target").HandlerFunc(csrf.Target)
router.NotFoundHandler = NewErrorHandler(404, "Requested resource could not be found.")
router.MethodNotAllowedHandler = NewErrorHandler(405, "Method not allowed.")
server := http.Server{
Addr: fmt.Sprintf(":%d", port),
Handler: router,
}
return &AppMockServer{
Server: &server,
port: port,
}
}
func (ams *AppMockServer) Start() {
log.Infof("Starting test server on port: %d", ams.port)
go func() {
log.Info(ams.Server.ListenAndServe())
}()
}
func (ams *AppMockServer) Kill() error {
return ams.Server.Shutdown(context.Background())
}
func StatusOK(w http.ResponseWriter, r *http.Request) {
successResponse(w)
w.Write([]byte("Ok"))
}
func successResponse(w http.ResponseWriter) {
w.WriteHeader(http.StatusOK)
}