web/server: stub /pollUpdates
This commit is contained in:
235
web/server.go
235
web/server.go
@@ -1,179 +1,186 @@
|
|||||||
package web
|
package web
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"code.severnaya.net/kordophone-mock/v2/server"
|
||||||
"code.severnaya.net/kordophone-mock/v2/server"
|
"github.com/rs/zerolog/log"
|
||||||
)
|
)
|
||||||
|
|
||||||
type MockHTTPServerConfiguration struct {
|
type MockHTTPServerConfiguration struct {
|
||||||
AuthEnabled bool
|
AuthEnabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type MockHTTPServer struct {
|
type MockHTTPServer struct {
|
||||||
Server server.Server
|
Server server.Server
|
||||||
mux http.ServeMux
|
mux http.ServeMux
|
||||||
authEnabled bool
|
authEnabled bool
|
||||||
}
|
}
|
||||||
|
|
||||||
type AuthError struct {
|
type AuthError struct {
|
||||||
message string
|
message string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (e *AuthError) Error() string {
|
func (e *AuthError) Error() string {
|
||||||
return e.message
|
return e.message
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockHTTPServer) logRequest(r *http.Request, extras ...string) {
|
func (m *MockHTTPServer) logRequest(r *http.Request, extras ...string) {
|
||||||
log.Debug().Msgf("%s %s %s", r.Method, r.URL.Path, strings.Join(extras, " "))
|
log.Debug().Msgf("%s %s %s", r.Method, r.URL.Path, strings.Join(extras, " "))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockHTTPServer) checkAuthentication(r *http.Request) error {
|
func (m *MockHTTPServer) checkAuthentication(r *http.Request) error {
|
||||||
if !m.authEnabled {
|
if !m.authEnabled {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for Authorization header
|
// Check for Authorization header
|
||||||
authHeader := r.Header.Get("Authorization")
|
authHeader := r.Header.Get("Authorization")
|
||||||
if authHeader == "" {
|
if authHeader == "" {
|
||||||
return &AuthError{"Missing Authorization header"}
|
return &AuthError{"Missing Authorization header"}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for "Bearer" prefix
|
// Check for "Bearer" prefix
|
||||||
if authHeader[:7] != "Bearer " {
|
if authHeader[:7] != "Bearer " {
|
||||||
return &AuthError{"Invalid Authorization header"}
|
return &AuthError{"Invalid Authorization header"}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for valid token
|
// Check for valid token
|
||||||
token := authHeader[7:]
|
token := authHeader[7:]
|
||||||
if !m.Server.CheckBearerToken(token) {
|
if !m.Server.CheckBearerToken(token) {
|
||||||
return &AuthError{"Invalid token"}
|
return &AuthError{"Invalid token"}
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockHTTPServer) handleVersion(w http.ResponseWriter, r *http.Request) {
|
func (m *MockHTTPServer) handleVersion(w http.ResponseWriter, r *http.Request) {
|
||||||
fmt.Fprintf(w, "%s", m.Server.Version())
|
fmt.Fprintf(w, "%s", m.Server.Version())
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockHTTPServer) handleStatus(w http.ResponseWriter, r *http.Request) {
|
func (m *MockHTTPServer) handleStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
if err := m.checkAuthentication(r); err != nil {
|
if err := m.checkAuthentication(r); err != nil {
|
||||||
log.Error().Err(err).Msg("Status: Error checking authentication")
|
log.Error().Err(err).Msg("Status: Error checking authentication")
|
||||||
http.Error(w, err.Error(), http.StatusUnauthorized)
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(w, "OK")
|
fmt.Fprintf(w, "OK")
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockHTTPServer) handleConversations(w http.ResponseWriter, r *http.Request) {
|
func (m *MockHTTPServer) handleConversations(w http.ResponseWriter, r *http.Request) {
|
||||||
convos := m.Server.Conversations()
|
convos := m.Server.Conversations()
|
||||||
|
|
||||||
// Encode convos as JSON
|
// Encode convos as JSON
|
||||||
jsonData, err := json.Marshal(convos)
|
jsonData, err := json.Marshal(convos)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Error marshalling conversations")
|
log.Error().Err(err).Msg("Error marshalling conversations")
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write JSON to response
|
// Write JSON to response
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.Write(jsonData)
|
w.Write(jsonData)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockHTTPServer) handleMessages(w http.ResponseWriter, r *http.Request) {
|
func (m *MockHTTPServer) handleMessages(w http.ResponseWriter, r *http.Request) {
|
||||||
// TODO handle optional "limit", "beforeDate", "beforeMessageGUID", and "afterMessageGUID" parameters
|
// TODO handle optional "limit", "beforeDate", "beforeMessageGUID", and "afterMessageGUID" parameters
|
||||||
|
|
||||||
guid := r.URL.Query().Get("guid")
|
guid := r.URL.Query().Get("guid")
|
||||||
if len(guid) == 0 {
|
if len(guid) == 0 {
|
||||||
log.Error().Msg("handleMessage: Got empty guid parameter")
|
log.Error().Msg("handleMessage: Got empty guid parameter")
|
||||||
http.Error(w, "no guid parameter specified", http.StatusBadRequest)
|
http.Error(w, "no guid parameter specified", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
conversation, err := m.Server.ConversationForGUID(guid)
|
conversation, err := m.Server.ConversationForGUID(guid)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msgf("handleMessage: Error getting conversation (%s)", guid)
|
log.Error().Err(err).Msgf("handleMessage: Error getting conversation (%s)", guid)
|
||||||
http.Error(w, "conversation not found", http.StatusBadRequest)
|
http.Error(w, "conversation not found", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
messages := m.Server.MessagesForConversation(*conversation)
|
messages := m.Server.MessagesForConversation(*conversation)
|
||||||
|
|
||||||
jsonData, err := json.Marshal(messages)
|
jsonData, err := json.Marshal(messages)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Error marshalling messages")
|
log.Error().Err(err).Msg("Error marshalling messages")
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write JSON to response
|
// Write JSON to response
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
w.Write(jsonData)
|
w.Write(jsonData)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockHTTPServer) handleAuthenticate(w http.ResponseWriter, r *http.Request) {
|
func (m *MockHTTPServer) handleAuthenticate(w http.ResponseWriter, r *http.Request) {
|
||||||
// Decode request body as AuthenticationRequest
|
// Decode request body as AuthenticationRequest
|
||||||
var authReq AuthenticationRequest
|
var authReq AuthenticationRequest
|
||||||
err := json.NewDecoder(r.Body).Decode(&authReq)
|
err := json.NewDecoder(r.Body).Decode(&authReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Authenticate: Error decoding request body")
|
log.Error().Err(err).Msg("Authenticate: Error decoding request body")
|
||||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Authenticate
|
// Authenticate
|
||||||
token, err := m.Server.Authenticate(authReq.Username, authReq.Password)
|
token, err := m.Server.Authenticate(authReq.Username, authReq.Password)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Authenticate: Error authenticating")
|
log.Error().Err(err).Msg("Authenticate: Error authenticating")
|
||||||
http.Error(w, err.Error(), http.StatusUnauthorized)
|
http.Error(w, err.Error(), http.StatusUnauthorized)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write response
|
// Write response
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
|
|
||||||
// Encode token as JSON
|
// Encode token as JSON
|
||||||
jsonData, err := json.Marshal(token)
|
jsonData, err := json.Marshal(token)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Error().Err(err).Msg("Error marshalling token")
|
log.Error().Err(err).Msg("Error marshalling token")
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Write JSON to response
|
// Write JSON to response
|
||||||
w.Write(jsonData)
|
w.Write(jsonData)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockHTTPServer) handleNotFound(w http.ResponseWriter, r *http.Request) {
|
func (m *MockHTTPServer) handleNotFound(w http.ResponseWriter, r *http.Request) {
|
||||||
log.Error().Msgf("Unimplemented API endpoint: %s %s", r.Method, r.URL.Path)
|
log.Error().Msgf("Unimplemented API endpoint: %s %s", r.Method, r.URL.Path)
|
||||||
http.NotFound(w, r)
|
http.NotFound(w, r)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *MockHTTPServer) handlePollUpdates(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// Stub: return 205 (Nothing to report)
|
||||||
|
w.WriteHeader(http.StatusResetContent)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *MockHTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
func (m *MockHTTPServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||||
m.logRequest(r, r.URL.Query().Encode())
|
m.logRequest(r, r.URL.Query().Encode())
|
||||||
m.mux.ServeHTTP(w, r)
|
m.mux.ServeHTTP(w, r)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMockHTTPServer(config MockHTTPServerConfiguration) *MockHTTPServer {
|
func NewMockHTTPServer(config MockHTTPServerConfiguration) *MockHTTPServer {
|
||||||
this := MockHTTPServer{
|
this := MockHTTPServer{
|
||||||
Server: *server.NewServer(),
|
Server: *server.NewServer(),
|
||||||
mux: *http.NewServeMux(),
|
mux: *http.NewServeMux(),
|
||||||
authEnabled: config.AuthEnabled,
|
authEnabled: config.AuthEnabled,
|
||||||
}
|
}
|
||||||
|
|
||||||
this.mux.Handle("/version", http.HandlerFunc(this.handleVersion))
|
this.mux.Handle("/version", http.HandlerFunc(this.handleVersion))
|
||||||
this.mux.Handle("/conversations", http.HandlerFunc(this.handleConversations))
|
this.mux.Handle("/conversations", http.HandlerFunc(this.handleConversations))
|
||||||
this.mux.Handle("/status", http.HandlerFunc(this.handleStatus))
|
this.mux.Handle("/status", http.HandlerFunc(this.handleStatus))
|
||||||
this.mux.Handle("/authenticate", http.HandlerFunc(this.handleAuthenticate))
|
this.mux.Handle("/authenticate", http.HandlerFunc(this.handleAuthenticate))
|
||||||
this.mux.Handle("/messages", http.HandlerFunc(this.handleMessages))
|
this.mux.Handle("/messages", http.HandlerFunc(this.handleMessages))
|
||||||
this.mux.Handle("/", http.HandlerFunc(this.handleNotFound))
|
this.mux.Handle("/pollUpdates", http.HandlerFunc(this.handlePollUpdates))
|
||||||
|
|
||||||
return &this
|
this.mux.Handle("/", http.HandlerFunc(this.handleNotFound))
|
||||||
|
|
||||||
|
return &this
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user