Skip to content

Commit 53bbe6e

Browse files
authored
feat: invalid jsonent can be shared (#39)
Sharing invalid Jsonnet is a valid action. Prior to this, the evaluated snippet was checked to ensure validity and this would force the snippet to be valid before sharing. Being able to share invalid Jsonnet for examples should also be allowed too. Now the invalid Jsonnet is simply logged.
1 parent 5e93b2e commit 53bbe6e

File tree

2 files changed

+6
-13
lines changed

2 files changed

+6
-13
lines changed

internal/server/backend_routes.go

+1-4
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,7 @@ func (srv *PlaygroundServer) HandleCreateShare() http.HandlerFunc {
6969
incomingJsonnet := r.FormValue("jsonnet-input")
7070
_, err := srv.State.EvaluateSnippet(incomingJsonnet)
7171
if err != nil {
72-
srv.State.Logger.Error("invalid share", "jsonnet", incomingJsonnet)
73-
// TODO: display an error for the bad req rather than using a 200
74-
_, _ = w.Write([]byte("Share is not available for invalid Jsonnet. Run your snippet to see the result."))
75-
return
72+
srv.State.Logger.Error("invalid jsonnet for share", "jsonnet", incomingJsonnet)
7673
}
7774

7875
snippetHash := hex.EncodeToString(srv.State.Hasher.Sum([]byte(incomingJsonnet)))[:15]

internal/server/backend_routes_test.go

+5-9
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ func TestHandleCreateShare(t *testing.T) {
7373
input string
7474
shouldFail bool
7575
}{
76-
{name: "hello-world", input: "{hello: 'world'}", shouldFail: false},
77-
{name: "blank", input: "{}", shouldFail: false},
78-
{name: "invalid-jsonnet", input: "{", shouldFail: true},
79-
{name: "invalid-jsonnet-2", input: "{hello:}", shouldFail: true},
80-
{name: "kubecfg", input: "local kubecfg = import 'internal:///kubecfg.libsonnet';\n{k8s: kubecfg.isK8sObject({apiVersion: 'v1', kind: 'Pod', spec: {}})}", shouldFail: false},
76+
{name: "hello-world", input: "{hello: 'world'}"},
77+
{name: "blank", input: "{}"},
78+
{name: "invalid-jsonnet", input: "{"},
79+
{name: "invalid-jsonnet-2", input: "{hello:}"},
80+
{name: "kubecfg", input: "local kubecfg = import 'internal:///kubecfg.libsonnet';\n{k8s: kubecfg.isK8sObject({apiVersion: 'v1', kind: 'Pod', spec: {}})}"},
8181
}
8282

8383
for _, tc := range tests {
@@ -92,10 +92,6 @@ func TestHandleCreateShare(t *testing.T) {
9292
handler := srv.HandleCreateShare()
9393
handler.ServeHTTP(rec, req)
9494

95-
if tc.shouldFail {
96-
assert.Contains(t, rec.Body.String(), "Share is not available for invalid Jsonnet")
97-
return
98-
}
9995
snippetHash := hex.EncodeToString(sha512.New().Sum([]byte(tc.input)))[:15]
10096
expected := fmt.Sprintf("Link: https://example.com/share/%s", snippetHash)
10197
assert.Equal(t, rec.Body.String(), expected, "expected: %s, got: %s", tc.name, expected, rec.Body.String())

0 commit comments

Comments
 (0)