-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2c1879d
commit b671c89
Showing
4 changed files
with
195 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"net/http" | ||
"sync" | ||
) | ||
|
||
func main() { | ||
h := newHandler() | ||
http.HandleFunc("/reset", h.reset) | ||
http.HandleFunc("/configure", h.configure) | ||
http.HandleFunc("/", h.catchAll) | ||
err := http.ListenAndServe(":8080", nil) | ||
if err != nil { | ||
log.Fatal(err) | ||
} | ||
} | ||
|
||
type handler struct { | ||
mu sync.RWMutex | ||
endpoints map[string]any | ||
} | ||
|
||
func newHandler() *handler { | ||
return &handler{ | ||
endpoints: map[string]any{}, | ||
} | ||
} | ||
|
||
func (h *handler) reset(w http.ResponseWriter, r *http.Request) { | ||
h.endpoints = map[string]any{} | ||
w.WriteHeader(http.StatusOK) | ||
} | ||
|
||
// EndpointConfigurationRequest is the request body for the /configure endpoint. | ||
type EndpointConfigurationRequest struct { | ||
Path string `json:"path"` | ||
Response any `json:"response"` | ||
} | ||
|
||
func (h *handler) configure(w http.ResponseWriter, r *http.Request) { | ||
if r.Method != http.MethodPost { | ||
http.Error(w, "Invalid request method", http.StatusMethodNotAllowed) | ||
return | ||
} | ||
|
||
request := EndpointConfigurationRequest{} | ||
if err := json.NewDecoder(r.Body).Decode(&request); err != nil { | ||
http.Error(w, "Invalid request body", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if request.Path == "" { | ||
http.Error(w, "Path is required", http.StatusBadRequest) | ||
return | ||
} | ||
|
||
if request.Response == nil { | ||
http.Error(w, "Response is required", http.StatusBadRequest) | ||
return | ||
} | ||
fmt.Println("HIIII", request.Response) | ||
h.mu.Lock() | ||
h.endpoints[request.Path] = request.Response | ||
h.mu.Unlock() | ||
|
||
w.WriteHeader(http.StatusOK) | ||
} | ||
|
||
func (h *handler) catchAll(w http.ResponseWriter, r *http.Request) { | ||
h.mu.RLock() | ||
response, exists := h.endpoints[r.URL.Path] | ||
h.mu.RUnlock() | ||
|
||
if !exists { | ||
http.NotFound(w, r) | ||
return | ||
} | ||
|
||
b, err := json.Marshal(response) | ||
if err != nil { | ||
http.Error(w, "Internal server error", http.StatusInternalServerError) | ||
return | ||
} | ||
|
||
_, err = w.Write(b) | ||
if err != nil { | ||
log.Default().Println("Failed to write response:", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package main | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestResetHandler(t *testing.T) { | ||
// given | ||
handler := newHandler() | ||
handler.endpoints["/test"] = "test" | ||
assert.Len(t, handler.endpoints, 1) | ||
|
||
// when | ||
httptestRecorder := httptest.NewRecorder() | ||
httptestRequest := httptest.NewRequest("GET", "/reset", nil) | ||
|
||
// then | ||
handler.reset(httptestRecorder, httptestRequest) | ||
assert.Len(t, handler.endpoints, 0) | ||
assert.Equal(t, http.StatusOK, httptestRecorder.Code) | ||
} | ||
|
||
func TestConfigureHandler(t *testing.T) { | ||
// given | ||
handler := newHandler() | ||
assert.Len(t, handler.endpoints, 0) | ||
|
||
// when | ||
httptestRecorder := httptest.NewRecorder() | ||
httptestRequest := httptest.NewRequest("POST", "/configure", bytes.NewBuffer([]byte(`{"path": "/test", "response": {"foo": "bar"}}`))) | ||
|
||
// then | ||
handler.configure(httptestRecorder, httptestRequest) | ||
assert.Len(t, handler.endpoints, 1) | ||
b, err := json.Marshal(handler.endpoints["/test"]) | ||
assert.NoError(t, err) | ||
assert.Equal(t, []byte(`{"foo":"bar"}`), b) | ||
assert.Equal(t, http.StatusOK, httptestRecorder.Code) | ||
} | ||
|
||
func TestConfigureHandlerInvalidMethod(t *testing.T) { | ||
// given | ||
handler := newHandler() | ||
|
||
// when | ||
httptestRecorder := httptest.NewRecorder() | ||
httptestRequest := httptest.NewRequest("GET", "/configure", nil) | ||
|
||
// then | ||
handler.configure(httptestRecorder, httptestRequest) | ||
assert.Equal(t, http.StatusMethodNotAllowed, httptestRecorder.Code) | ||
} | ||
|
||
func TestCatchAllHandler(t *testing.T) { | ||
// given | ||
handler := newHandler() | ||
|
||
response := struct { | ||
Foo string `json:"foo"` | ||
}{ | ||
Foo: "bar", | ||
} | ||
|
||
handler.endpoints["/test"] = response | ||
|
||
// when | ||
httptestRecorder := httptest.NewRecorder() | ||
httptestRequest := httptest.NewRequest("GET", "/test", nil) | ||
|
||
// then | ||
handler.catchAll(httptestRecorder, httptestRequest) | ||
assert.Equal(t, []byte(`{"foo":"bar"}`), httptestRecorder.Body.Bytes()) | ||
assert.Equal(t, http.StatusOK, httptestRecorder.Code) | ||
} | ||
|
||
func TestCatchAllHandlerNotFound(t *testing.T) { | ||
// given | ||
handler := newHandler() | ||
|
||
// when | ||
httptestRecorder := httptest.NewRecorder() | ||
httptestRequest := httptest.NewRequest("GET", "/test", nil) | ||
|
||
// then | ||
handler.catchAll(httptestRecorder, httptestRequest) | ||
assert.Equal(t, http.StatusNotFound, httptestRecorder.Code) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters