package ocapi import ( "context" "fmt" "net/http" "net/http/httptest" "net/url" "testing" ) func TestRawFile(t *testing.T) { var gotPath, gotDir string srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path != "/api/fs/raw" { t.Errorf("path = %q", r.URL.Path) } if got := r.Header.Get("x-opencode-directory-encoding"); got != "uri" { t.Errorf("directory encoding = %q", got) } gotPath = r.URL.Query().Get("path") if got := r.URL.Query().Get("allowOutsideWorkspace"); got != "true" { t.Errorf("allowOutsideWorkspace = %q", got) } gotDir, _ = url.PathUnescape(r.Header.Get("x-opencode-directory")) w.Header().Set("Content-Type", "image/png") fmt.Fprint(w, "PNGDATA") })) defer srv.Close() c, err := New(srv.URL, Options{}) if err != nil { t.Fatal(err) } data, mime, err := c.RawFile(context.Background(), "/work/a.png", "/work") if err != nil { t.Fatal(err) } if string(data) != "PNGDATA" { t.Errorf("data = %q", data) } if mime != "image/png" { t.Errorf("mime = %q", mime) } if gotPath != "/work/a.png" { t.Errorf("query path = %q", gotPath) } if gotDir != "/work" { t.Errorf("directory = %q", gotDir) } } func TestRawFileError(t *testing.T) { srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { http.Error(w, "nope", http.StatusNotFound) })) defer srv.Close() c, _ := New(srv.URL, Options{}) if _, _, err := c.RawFile(context.Background(), "/work/missing.png", ""); err == nil { t.Fatal("expected an error") } }