-
Notifications
You must be signed in to change notification settings - Fork 65
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
b22f2bd
commit 4f8efd9
Showing
7 changed files
with
154 additions
and
53 deletions.
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
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,77 @@ | ||
package api_test | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"io" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/cybertec-postgresql/pg_timetable/internal/api" | ||
"github.com/cybertec-postgresql/pg_timetable/internal/config" | ||
"github.com/cybertec-postgresql/pg_timetable/internal/log" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
type apihandler struct { | ||
} | ||
|
||
func (r *apihandler) IsReady() bool { | ||
return true | ||
} | ||
|
||
func (sch *apihandler) StartChain(ctx context.Context, chainId int) error { | ||
if chainId == 0 { | ||
return errors.New("invalid chain id") | ||
} | ||
return nil | ||
} | ||
|
||
func (sch *apihandler) StopChain(ctx context.Context, chainId int) error { | ||
return nil | ||
} | ||
|
||
var restsrv *api.RestApiServer | ||
|
||
func init() { | ||
restsrv = api.Init(config.RestApiOpts{Port: 8080}, log.Init(config.LoggingOpts{LogLevel: "error"})) | ||
} | ||
|
||
func TestStatus(t *testing.T) { | ||
|
||
r, err := http.Get("http://localhost:8080/liveness") | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, r.StatusCode) | ||
|
||
r, err = http.Get("http://localhost:8080/readiness") | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusServiceUnavailable, r.StatusCode) | ||
|
||
restsrv.ApiHandler = &apihandler{} | ||
r, err = http.Get("http://localhost:8080/readiness") | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, r.StatusCode) | ||
} | ||
|
||
func TestChainManager(t *testing.T) { | ||
restsrv.ApiHandler = &apihandler{} | ||
r, err := http.Get("http://localhost:8080/startchain") | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusBadRequest, r.StatusCode) | ||
b, _ := io.ReadAll(r.Body) | ||
assert.Contains(t, string(b), "invalid syntax") | ||
|
||
r, err = http.Get("http://localhost:8080/startchain?id=1") | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, r.StatusCode) | ||
|
||
r, err = http.Get("http://localhost:8080/stopchain?id=1") | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusOK, r.StatusCode) | ||
|
||
r, err = http.Get("http://localhost:8080/startchain?id=0") | ||
assert.NoError(t, err) | ||
assert.Equal(t, http.StatusBadRequest, r.StatusCode) | ||
b, _ = io.ReadAll(r.Body) | ||
assert.Contains(t, string(b), "invalid chain id") | ||
} |
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,26 @@ | ||
package api | ||
|
||
import ( | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
func (Server *RestApiServer) chainHandler(w http.ResponseWriter, r *http.Request) { | ||
Server.l.Debugf("Received /%s REST API request", r.URL.Path) | ||
chainID, err := strconv.Atoi(r.URL.Query().Get("id")) | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
switch r.URL.Path { | ||
case "/startchain": | ||
err = Server.ApiHandler.StartChain(r.Context(), chainID) | ||
case "/stopchain": | ||
err = Server.ApiHandler.StopChain(r.Context(), chainID) | ||
} | ||
if err != nil { | ||
http.Error(w, err.Error(), http.StatusBadRequest) | ||
return | ||
} | ||
w.WriteHeader(http.StatusOK) | ||
} |
This file was deleted.
Oops, something went wrong.
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
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