(t *testing.T)
| 110 | } |
| 111 | |
| 112 | func TestReadFile(t *testing.T) { |
| 113 | t.Parallel() |
| 114 | |
| 115 | tmpdir := os.TempDir() |
| 116 | noPermsFilePath := filepath.Join(tmpdir, "no-perms") |
| 117 | |
| 118 | logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug) |
| 119 | fs := newTestFs(afero.NewMemMapFs(), func(call, file string) error { |
| 120 | if file == noPermsFilePath { |
| 121 | return os.ErrPermission |
| 122 | } |
| 123 | return nil |
| 124 | }) |
| 125 | api := agentfiles.NewAPI(logger, fs, nil) |
| 126 | |
| 127 | dirPath := filepath.Join(tmpdir, "a-directory") |
| 128 | err := fs.MkdirAll(dirPath, 0o755) |
| 129 | require.NoError(t, err) |
| 130 | |
| 131 | filePath := filepath.Join(tmpdir, "file") |
| 132 | err = afero.WriteFile(fs, filePath, []byte("content"), 0o644) |
| 133 | require.NoError(t, err) |
| 134 | |
| 135 | imagePath := filepath.Join(tmpdir, "file.png") |
| 136 | err = afero.WriteFile(fs, imagePath, []byte("not really an image"), 0o644) |
| 137 | require.NoError(t, err) |
| 138 | |
| 139 | tests := []struct { |
| 140 | name string |
| 141 | path string |
| 142 | limit int64 |
| 143 | offset int64 |
| 144 | bytes []byte |
| 145 | mimeType string |
| 146 | errCode int |
| 147 | error string |
| 148 | }{ |
| 149 | { |
| 150 | name: "NoPath", |
| 151 | path: "", |
| 152 | errCode: http.StatusBadRequest, |
| 153 | error: "\"path\" is required", |
| 154 | }, |
| 155 | { |
| 156 | name: "RelativePathDotSlash", |
| 157 | path: "./relative", |
| 158 | errCode: http.StatusBadRequest, |
| 159 | error: "file path must be absolute", |
| 160 | }, |
| 161 | { |
| 162 | name: "RelativePath", |
| 163 | path: "also-relative", |
| 164 | errCode: http.StatusBadRequest, |
| 165 | error: "file path must be absolute", |
| 166 | }, |
| 167 | { |
| 168 | name: "NegativeLimit", |
| 169 | path: filePath, |
nothing calls this directly
no test coverage detected