TestCompositeBackend_EdgeCases 测试边界情况
(t *testing.T)
| 211 | |
| 212 | // TestCompositeBackend_EdgeCases 测试边界情况 |
| 213 | func TestCompositeBackend_EdgeCases(t *testing.T) { |
| 214 | ctx := context.Background() |
| 215 | |
| 216 | defaultBackend := NewStateBackend() |
| 217 | specialBackend := NewStateBackend() |
| 218 | |
| 219 | composite := NewCompositeBackend(defaultBackend, []RouteConfig{ |
| 220 | {Prefix: "/special/", Backend: specialBackend}, |
| 221 | }) |
| 222 | |
| 223 | // 测试根路径 "/" |
| 224 | t.Run("Root path", func(t *testing.T) { |
| 225 | _, err := composite.Write(ctx, "/special/", "root content") |
| 226 | if err != nil { |
| 227 | t.Fatalf("Write to root failed: %v", err) |
| 228 | } |
| 229 | |
| 230 | // 应该在 specialBackend 中作为 "/" |
| 231 | content, err := specialBackend.Read(ctx, "/", 0, 0) |
| 232 | if err != nil { |
| 233 | t.Fatalf("Read root from specialBackend failed: %v", err) |
| 234 | } |
| 235 | |
| 236 | if content != "root content" { |
| 237 | t.Errorf("Expected 'root content', got '%s'", content) |
| 238 | } |
| 239 | }) |
| 240 | |
| 241 | // 测试前缀不以 / 结尾的情况(虽然不推荐) |
| 242 | t.Run("Path without leading slash after strip", func(t *testing.T) { |
| 243 | backend2 := NewStateBackend() |
| 244 | composite2 := NewCompositeBackend(defaultBackend, []RouteConfig{ |
| 245 | {Prefix: "/prefix", Backend: backend2}, // 注意没有尾部 / |
| 246 | }) |
| 247 | |
| 248 | _, err := composite2.Write(ctx, "/prefixfile.txt", "content") |
| 249 | if err != nil { |
| 250 | t.Fatalf("Write failed: %v", err) |
| 251 | } |
| 252 | |
| 253 | // 应该在 backend2 中作为 "/file.txt" |
| 254 | content, err := backend2.Read(ctx, "/file.txt", 0, 0) |
| 255 | if err != nil { |
| 256 | t.Fatalf("Read failed: %v", err) |
| 257 | } |
| 258 | |
| 259 | if content != "content" { |
| 260 | t.Errorf("Expected 'content', got '%s'", content) |
| 261 | } |
| 262 | }) |
| 263 | } |
nothing calls this directly
no test coverage detected