MCPcopy Create free account
hub / github.com/coder/agentapi / TestServer_UploadFiles_Errors

Function TestServer_UploadFiles_Errors

lib/httpapi/server_test.go:816–959  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

814}
815
816func TestServer_UploadFiles_Errors(t *testing.T) {
817 t.Parallel()
818 ctx := logctx.WithLogger(context.Background(), slog.New(slog.NewTextHandler(os.Stdout, nil)))
819 srv, err := httpapi.NewServer(ctx, httpapi.ServerConfig{
820 AgentType: msgfmt.AgentTypeClaude,
821 AgentIO: nil,
822 Port: 0,
823 ChatBasePath: "/chat",
824 AllowedHosts: []string{"*"},
825 AllowedOrigins: []string{"*"},
826 })
827 require.NoError(t, err)
828 tsServer := httptest.NewServer(srv.Handler())
829 t.Cleanup(tsServer.Close)
830
831 t.Run("missing file field", func(t *testing.T) {
832 t.Parallel()
833
834 // Create multipart form without file field
835 var buf bytes.Buffer
836 writer := multipart.NewWriter(&buf)
837 err := writer.WriteField("notfile", "value")
838 require.NoError(t, err)
839 err = writer.Close()
840 require.NoError(t, err)
841
842 req, err := http.NewRequest("POST", tsServer.URL+"/upload", &buf)
843 require.NoError(t, err)
844 req.Header.Set("Content-Type", writer.FormDataContentType())
845
846 client := &http.Client{}
847 resp, err := client.Do(req)
848 require.NoError(t, err)
849 t.Cleanup(func() {
850 _ = resp.Body.Close()
851 })
852
853 require.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
854 })
855
856 t.Run("invalid content type", func(t *testing.T) {
857 t.Parallel()
858
859 req, err := http.NewRequest("POST", tsServer.URL+"/upload", strings.NewReader("not multipart"))
860 require.NoError(t, err)
861 req.Header.Set("Content-Type", "application/json")
862
863 client := &http.Client{}
864 resp, err := client.Do(req)
865 require.NoError(t, err)
866 t.Cleanup(func() {
867 _ = resp.Body.Close()
868 })
869
870 require.Equal(t, http.StatusUnprocessableEntity, resp.StatusCode)
871 })
872
873 t.Run("file size exactly 10MB", func(t *testing.T) {

Callers

nothing calls this directly

Calls 6

HandlerMethod · 0.95
WithLoggerFunction · 0.92
NewServerFunction · 0.92
CloseMethod · 0.80
DoMethod · 0.65
WriteMethod · 0.65

Tested by

no test coverage detected