Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion pkg/web/web.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2025, Command Line Inc.
// Copyright 2026, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0

package web
Expand Down Expand Up @@ -119,12 +119,14 @@ func handleService(w http.ResponseWriter, r *http.Request) {
err = json.Unmarshal(bodyData, &webCall)
if err != nil {
http.Error(w, fmt.Sprintf("invalid request body: %v", err), http.StatusBadRequest)
return
}

rtn := service.CallService(r.Context(), webCall)
jsonRtn, err := json.Marshal(rtn)
if err != nil {
http.Error(w, fmt.Sprintf("error serializing response: %v", err), http.StatusInternalServerError)
return
}
w.Header().Set(ContentTypeHeaderKey, ContentTypeJson)
w.Header().Set(ContentLengthHeaderKey, fmt.Sprintf("%d", len(jsonRtn)))
Expand Down Expand Up @@ -157,6 +159,7 @@ func handleWaveFile(w http.ResponseWriter, r *http.Request) {
offset, err = strconv.ParseInt(offsetStr, 10, 64)
if err != nil {
http.Error(w, fmt.Sprintf("invalid offset: %v", err), http.StatusBadRequest)
return
}
}
if _, err := uuid.Parse(zoneId); err != nil {
Expand All @@ -180,6 +183,7 @@ func handleWaveFile(w http.ResponseWriter, r *http.Request) {
jsonFileBArr, err := json.Marshal(file)
if err != nil {
http.Error(w, fmt.Sprintf("error serializing file info: %v", err), http.StatusInternalServerError)
return
}
// can make more efficient by checking modtime + If-Modified-Since headers to allow caching
dataStartIdx := file.DataStartIdx()
Expand Down Expand Up @@ -239,6 +243,7 @@ func handleLocalStreamFile(w http.ResponseWriter, r *http.Request, path string,
path, err := wavebase.ExpandHomeDir(path)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
http.ServeFile(w, r, path)
}
Expand Down
81 changes: 81 additions & 0 deletions pkg/web/web_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// Copyright 2026, Command Line Inc.
// SPDX-License-Identifier: Apache-2.0

package web

import (
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"

"github.com/wavetermdev/waveterm/pkg/service"
)

type handlerTestService struct {
calls int
}

func (svc *handlerTestService) Invoke() {
svc.calls++
}

func (svc *handlerTestService) Unserializable() chan int {
return make(chan int)
}

func TestHandleServiceRejectsInvalidBodyWithoutCallingService(t *testing.T) {
svc := &handlerTestService{}
service.ServiceMap["handler-test"] = svc
t.Cleanup(func() { delete(service.ServiceMap, "handler-test") })

body := `{"service":"handler-test","method":"Invoke","args":[],"uicontext":123}`
var partialCall service.WebCallType
if err := json.Unmarshal([]byte(body), &partialCall); err == nil || partialCall.Service != "handler-test" || partialCall.Method != "Invoke" {
t.Fatalf("invalid test body did not partially decode a service call: %+v, %v", partialCall, err)
}

req := httptest.NewRequest(http.MethodPost, "/wave/service", strings.NewReader(body))
resp := httptest.NewRecorder()
handleService(resp, req)

if resp.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want 400", resp.Code)
}
if svc.calls != 0 {
t.Fatalf("service invoked %d times for invalid body", svc.calls)
}
if !strings.HasPrefix(resp.Body.String(), "invalid request body:") || strings.Count(resp.Body.String(), "\n") != 1 {
t.Fatalf("unexpected response body: %q", resp.Body.String())
}
}

func TestHandleServiceStopsAfterMarshalError(t *testing.T) {
service.ServiceMap["handler-test"] = &handlerTestService{}
t.Cleanup(func() { delete(service.ServiceMap, "handler-test") })

req := httptest.NewRequest(http.MethodPost, "/wave/service", strings.NewReader(`{"service":"handler-test","method":"Unserializable","args":[]}`))
resp := httptest.NewRecorder()
handleService(resp, req)

if resp.Code != http.StatusInternalServerError {
t.Fatalf("status = %d, want 500", resp.Code)
}
if got := resp.Header().Get(ContentLengthHeaderKey); got != "" {
t.Fatalf("Content-Length = %q after error response, want unset", got)
}
}

func TestHandleWaveFileRejectsInvalidOffsetFirst(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/wave/file?offset=invalid&zoneid=invalid&name=test", nil)
resp := httptest.NewRecorder()
handleWaveFile(resp, req)

if resp.Code != http.StatusBadRequest {
t.Fatalf("status = %d, want 400", resp.Code)
}
if !strings.HasPrefix(resp.Body.String(), "invalid offset:") || strings.Count(resp.Body.String(), "\n") != 1 {
t.Fatalf("unexpected response body: %q", resp.Body.String())
}
}
Loading