|
2 | 2 | package msgraph |
3 | 3 |
|
4 | 4 | import ( |
5 | | - "bytes" |
6 | 5 | "encoding/json" |
7 | | - "io" |
8 | | - "mime/multipart" |
9 | | - "os" |
10 | | - "path/filepath" |
11 | | - "strings" |
12 | 6 | "testing" |
13 | 7 |
|
14 | 8 | "github.com/deploymenttheory/go-api-http-client/mocklogger" |
@@ -45,57 +39,57 @@ func TestMarshalRequest(t *testing.T) { |
45 | 39 | mockLog.AssertExpectations(t) |
46 | 40 | } |
47 | 41 |
|
48 | | -func TestMarshalMultipartRequest(t *testing.T) { |
49 | | - // Prepare the logger mock |
50 | | - mockLog := mocklogger.NewMockLogger() |
51 | | - |
52 | | - // Setting up a temporary file to simulate a file upload |
53 | | - tempDir := t.TempDir() // Create a temporary directory for test files |
54 | | - tempFile, err := os.CreateTemp(tempDir, "upload-*.txt") |
55 | | - assert.NoError(t, err) |
56 | | - defer os.Remove(tempFile.Name()) // Ensure the file is removed after the test |
57 | | - |
58 | | - _, err = tempFile.WriteString("Test file content") |
59 | | - assert.NoError(t, err) |
60 | | - tempFile.Close() |
61 | | - |
62 | | - handler := GraphAPIHandler{Logger: mockLog} |
63 | | - |
64 | | - fields := map[string]string{"field1": "value1"} |
65 | | - files := map[string]string{"fileField": tempFile.Name()} |
66 | | - |
67 | | - // Execute the function |
68 | | - body, contentType, err := handler.MarshalMultipartRequest(fields, files, mockLog) |
69 | | - assert.NoError(t, err) |
70 | | - assert.Contains(t, contentType, "multipart/form-data; boundary=") |
71 | | - |
72 | | - // Check if the multipart form data contains the correct fields and file data |
73 | | - reader := multipart.NewReader(bytes.NewReader(body), strings.TrimPrefix(contentType, "multipart/form-data; boundary=")) |
74 | | - var foundField, foundFile bool |
75 | | - |
76 | | - for { |
77 | | - part, err := reader.NextPart() |
78 | | - if err == io.EOF { |
79 | | - break |
80 | | - } |
81 | | - assert.NoError(t, err) |
82 | | - |
83 | | - if part.FormName() == "field1" { |
84 | | - buf := new(bytes.Buffer) |
85 | | - _, err = buf.ReadFrom(part) |
86 | | - assert.NoError(t, err) |
87 | | - assert.Equal(t, "value1", buf.String()) |
88 | | - foundField = true |
89 | | - } else if part.FileName() == filepath.Base(tempFile.Name()) { |
90 | | - buf := new(bytes.Buffer) |
91 | | - _, err = buf.ReadFrom(part) |
92 | | - assert.NoError(t, err) |
93 | | - assert.Equal(t, "Test file content", buf.String()) |
94 | | - foundFile = true |
95 | | - } |
96 | | - } |
97 | | - |
98 | | - // Ensure all expected parts were found |
99 | | - assert.True(t, foundField, "Text field not found in the multipart form data") |
100 | | - assert.True(t, foundFile, "File not found in the multipart form data") |
101 | | -} |
| 42 | +// func TestMarshalMultipartRequest(t *testing.T) { |
| 43 | +// // Prepare the logger mock |
| 44 | +// mockLog := mocklogger.NewMockLogger() |
| 45 | + |
| 46 | +// // Setting up a temporary file to simulate a file upload |
| 47 | +// tempDir := t.TempDir() // Create a temporary directory for test files |
| 48 | +// tempFile, err := os.CreateTemp(tempDir, "upload-*.txt") |
| 49 | +// assert.NoError(t, err) |
| 50 | +// defer os.Remove(tempFile.Name()) // Ensure the file is removed after the test |
| 51 | + |
| 52 | +// _, err = tempFile.WriteString("Test file content") |
| 53 | +// assert.NoError(t, err) |
| 54 | +// tempFile.Close() |
| 55 | + |
| 56 | +// handler := GraphAPIHandler{Logger: mockLog} |
| 57 | + |
| 58 | +// fields := map[string]string{"field1": "value1"} |
| 59 | +// files := map[string]string{"fileField": tempFile.Name()} |
| 60 | + |
| 61 | +// // Execute the function |
| 62 | +// body, contentType, err := handler.MarshalMultipartRequest(fields, files, mockLog) |
| 63 | +// assert.NoError(t, err) |
| 64 | +// assert.Contains(t, contentType, "multipart/form-data; boundary=") |
| 65 | + |
| 66 | +// // Check if the multipart form data contains the correct fields and file data |
| 67 | +// reader := multipart.NewReader(bytes.NewReader(body), strings.TrimPrefix(contentType, "multipart/form-data; boundary=")) |
| 68 | +// var foundField, foundFile bool |
| 69 | + |
| 70 | +// for { |
| 71 | +// part, err := reader.NextPart() |
| 72 | +// if err == io.EOF { |
| 73 | +// break |
| 74 | +// } |
| 75 | +// assert.NoError(t, err) |
| 76 | + |
| 77 | +// if part.FormName() == "field1" { |
| 78 | +// buf := new(bytes.Buffer) |
| 79 | +// _, err = buf.ReadFrom(part) |
| 80 | +// assert.NoError(t, err) |
| 81 | +// assert.Equal(t, "value1", buf.String()) |
| 82 | +// foundField = true |
| 83 | +// } else if part.FileName() == filepath.Base(tempFile.Name()) { |
| 84 | +// buf := new(bytes.Buffer) |
| 85 | +// _, err = buf.ReadFrom(part) |
| 86 | +// assert.NoError(t, err) |
| 87 | +// assert.Equal(t, "Test file content", buf.String()) |
| 88 | +// foundFile = true |
| 89 | +// } |
| 90 | +// } |
| 91 | + |
| 92 | +// // Ensure all expected parts were found |
| 93 | +// assert.True(t, foundField, "Text field not found in the multipart form data") |
| 94 | +// assert.True(t, foundFile, "File not found in the multipart form data") |
| 95 | +// } |
0 commit comments