-
Notifications
You must be signed in to change notification settings - Fork 11
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
Showing
2 changed files
with
87 additions
and
6 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,87 @@ | ||
package app_test | ||
|
||
import ( | ||
"net/http/httptest" | ||
"path" | ||
"testing" | ||
|
||
"github.com/claceio/clace/internal/app" | ||
"github.com/claceio/clace/internal/testutil" | ||
) | ||
|
||
func actionTester(t *testing.T, rootPath bool, actionPath string) { | ||
t.Helper() | ||
logger := testutil.TestLogger() | ||
fileData := map[string]string{ | ||
"app.star": ` | ||
def handler(dry_run, args): | ||
return ace.result(status="done", values=["a", "b"]) | ||
app = ace.app("testApp", | ||
actions=[ace.action("testAction", "` + actionPath + `", handler)]) | ||
`, | ||
"params.star": `param("param1", description="param1 description", type=STRING, default="myvalue")`, | ||
} | ||
var a *app.App | ||
var err error | ||
if rootPath { | ||
a, _, err = CreateTestAppRoot(logger, fileData) | ||
} else { | ||
a, _, err = CreateTestApp(logger, fileData) | ||
} | ||
if err != nil { | ||
t.Fatalf("Error %s", err) | ||
} | ||
|
||
var reqPath string | ||
if rootPath { | ||
reqPath = actionPath | ||
} else { | ||
reqPath = path.Join("/test", actionPath) | ||
} | ||
|
||
request := httptest.NewRequest("GET", reqPath, nil) | ||
response := httptest.NewRecorder() | ||
a.ServeHTTP(response, request) | ||
|
||
testutil.AssertEqualsInt(t, "code", 200, response.Code) | ||
testutil.AssertStringContains(t, response.Body.String(), "<title>testAction</title>") | ||
testutil.AssertStringContains(t, response.Body.String(), `id="param_param1"`) | ||
|
||
request = httptest.NewRequest("POST", reqPath, nil) | ||
response = httptest.NewRecorder() | ||
a.ServeHTTP(response, request) | ||
testutil.AssertEqualsInt(t, "code", 200, response.Code) | ||
testutil.AssertStringMatch(t, "match response", response.Body.String(), ` | ||
<div class="text-lg text-bold"> | ||
done | ||
</div> | ||
<div id="action_result" hx-swap-oob="true" hx-swap="outerHTML"> | ||
<div class="divider text-lg text-secondary">Output</div> | ||
<textarea | ||
rows="20" | ||
class="textarea textarea-success w-full font-mono" | ||
readonly>a | ||
b | ||
</textarea | ||
> | ||
</div> | ||
`) | ||
|
||
} | ||
|
||
func TestRootAppRootAction(t *testing.T) { | ||
actionTester(t, true, "/") | ||
} | ||
func TestRootApp(t *testing.T) { | ||
actionTester(t, true, "/abc") | ||
} | ||
|
||
func TestNonRootAppRootAction(t *testing.T) { | ||
actionTester(t, false, "/") | ||
} | ||
func TestNonRootApp(t *testing.T) { | ||
actionTester(t, false, "/abc") | ||
} |