| 268 | } |
| 269 | |
| 270 | func demonstrateFileUpload(client cycletls.CycleTLS) { |
| 271 | // Try to read an existing file, fallback to created data |
| 272 | var testFileData []byte |
| 273 | var fileName string |
| 274 | |
| 275 | testFiles := []string{"./examples/test-image.jpg", "./tests/images/test.jpeg", "./go.mod", "./package.json"} |
| 276 | |
| 277 | for _, filePath := range testFiles { |
| 278 | if data, err := os.ReadFile(filePath); err == nil { |
| 279 | testFileData = data |
| 280 | fileName = filePath |
| 281 | break |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | if testFileData == nil { |
| 286 | // Create minimal test file data |
| 287 | testFileData = []byte("Test file content with binary data: \x00\x01\x02\xFF\xFE") |
| 288 | fileName = "generated test data" |
| 289 | } |
| 290 | |
| 291 | hasher := md5.New() |
| 292 | hasher.Write(testFileData) |
| 293 | fileHash := hex.EncodeToString(hasher.Sum(nil)) |
| 294 | |
| 295 | fmt.Printf(" 📁 Using file: %s\n", fileName) |
| 296 | fmt.Printf(" File size: %d bytes\n", len(testFileData)) |
| 297 | fmt.Printf(" File hash: %s\n", fileHash) |
| 298 | |
| 299 | // Upload using BodyBytes |
| 300 | response, err := client.Do("https://httpbin.org/post", cycletls.Options{ |
| 301 | BodyBytes: testFileData, |
| 302 | Headers: map[string]string{ |
| 303 | "Content-Type": "application/octet-stream", |
| 304 | }, |
| 305 | }, "POST") |
| 306 | |
| 307 | if err != nil { |
| 308 | log.Printf("❌ Upload failed: %v", err) |
| 309 | return |
| 310 | } |
| 311 | |
| 312 | fmt.Printf(" ✅ Upload successful: %t\n", response.Status == 200) |
| 313 | |
| 314 | // Verify upload by parsing response |
| 315 | if response.Status == 200 { |
| 316 | var respData map[string]interface{} |
| 317 | if err := json.Unmarshal([]byte(response.Body), &respData); err == nil { |
| 318 | if _, hasData := respData["data"]; hasData { |
| 319 | fmt.Println(" ✅ Server received binary data") |
| 320 | } |
| 321 | } |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | func showMigrationExamples() { |
| 326 | fmt.Println("\n📋 Migration Guide - Before vs After Issue #297 Fix:\n") |