Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(server): add package for e2e tests
- Loading branch information
Showing
7 changed files
with
203 additions
and
5 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,70 @@ | ||
package e2e | ||
|
||
import ( | ||
"context" | ||
"net" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/gavv/httpexpect/v2" | ||
"github.com/reearth/reearth/server/internal/app" | ||
"github.com/reearth/reearth/server/internal/infrastructure/memory" | ||
"github.com/reearth/reearth/server/internal/infrastructure/mongo" | ||
"github.com/reearth/reearth/server/internal/usecase/gateway" | ||
"github.com/reearth/reearth/server/internal/usecase/repo" | ||
"github.com/reearth/reearthx/mongox/mongotest" | ||
"github.com/samber/lo" | ||
) | ||
|
||
func init() { | ||
mongotest.Env = "REEARTH_DB" | ||
} | ||
|
||
func StartServer(t *testing.T, cfg *app.Config, useMongo bool) *httpexpect.Expect { | ||
t.Helper() | ||
|
||
if testing.Short() { | ||
t.SkipNow() | ||
} | ||
|
||
ctx := context.Background() | ||
|
||
l, err := net.Listen("tcp", ":0") | ||
if err != nil { | ||
t.Fatalf("server failed to listen: %v", err) | ||
} | ||
|
||
var repos *repo.Container | ||
if useMongo { | ||
db := mongotest.Connect(t)(t) | ||
repos = lo.Must(mongo.New(ctx, db)) | ||
} else { | ||
repos = memory.New() | ||
} | ||
|
||
srv := app.NewServer(ctx, &app.ServerConfig{ | ||
Config: cfg, | ||
Repos: repos, | ||
Gateways: &gateway.Container{}, | ||
}) | ||
|
||
ch := make(chan error) | ||
go func() { | ||
if err := srv.Serve(l); err != http.ErrServerClosed { | ||
ch <- err | ||
} | ||
close(ch) | ||
}() | ||
|
||
t.Cleanup(func() { | ||
if err := srv.Shutdown(context.Background()); err != nil { | ||
t.Fatalf("server shutdown: %v", err) | ||
} | ||
|
||
if err := <-ch; err != nil { | ||
t.Fatalf("server serve: %v", err) | ||
} | ||
}) | ||
|
||
return httpexpect.New(t, "http://"+l.Addr().String()) | ||
} |
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,39 @@ | ||
package e2e | ||
|
||
import ( | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/reearth/reearth/server/internal/app" | ||
) | ||
|
||
func TestPingAPI(t *testing.T) { | ||
e := StartServer(t, &app.Config{ | ||
Origins: []string{"https://example.com"}, | ||
AuthSrv: app.AuthSrvConfig{ | ||
Disabled: true, | ||
}, | ||
}, false) | ||
|
||
e.OPTIONS("/api/ping"). | ||
WithHeader("Origin", "https://example.com"). | ||
Expect(). | ||
Status(http.StatusNoContent). | ||
Header("Access-Control-Allow-Origin"). | ||
Equal("https://example.com") | ||
|
||
r := e.GET("/api/ping"). | ||
WithHeader("Origin", "https://example.com"). | ||
Expect() | ||
|
||
r.Header("Cache-Control"). | ||
Equal("private, no-store, no-cache, must-revalidate") | ||
|
||
r.Header("Access-Control-Allow-Origin"). | ||
Equal("https://example.com") | ||
|
||
r.Status(http.StatusOK). | ||
JSON(). | ||
String(). | ||
Equal("pong") | ||
} |
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
Oops, something went wrong.