|
| 1 | +// apiintegrations/msgraph/msgraph_api_request_test.go |
| 2 | +package msgraph |
| 3 | + |
| 4 | +import ( |
| 5 | + "bytes" |
| 6 | + "encoding/json" |
| 7 | + "io" |
| 8 | + "mime/multipart" |
| 9 | + "os" |
| 10 | + "path/filepath" |
| 11 | + "strings" |
| 12 | + "testing" |
| 13 | + |
| 14 | + "github.com/deploymenttheory/go-api-http-client/mocklogger" |
| 15 | + "github.com/stretchr/testify/assert" |
| 16 | + "github.com/stretchr/testify/mock" |
| 17 | + "go.uber.org/zap" |
| 18 | +) |
| 19 | + |
| 20 | +// TestMarshalRequest tests the MarshalRequest function. |
| 21 | +func TestMarshalRequest(t *testing.T) { |
| 22 | + body := map[string]interface{}{ |
| 23 | + "name": "John Doe", |
| 24 | + "age": 30, |
| 25 | + } |
| 26 | + method := "POST" |
| 27 | + endpoint := "/users" |
| 28 | + mockLog := mocklogger.NewMockLogger() |
| 29 | + handler := GraphAPIHandler{Logger: mockLog} |
| 30 | + |
| 31 | + expectedData, _ := json.Marshal(body) |
| 32 | + |
| 33 | + // Correct the way we setup the logger mock |
| 34 | + mockLog.On("Debug", "JSON Request Body", mock.MatchedBy(func(fields []zap.Field) bool { |
| 35 | + if len(fields) != 1 { |
| 36 | + return false |
| 37 | + } |
| 38 | + return fields[0].Key == "Body" && fields[0].String == string(expectedData) |
| 39 | + })).Once() |
| 40 | + |
| 41 | + data, err := handler.MarshalRequest(body, method, endpoint, mockLog) |
| 42 | + |
| 43 | + assert.NoError(t, err) |
| 44 | + assert.Equal(t, expectedData, data) |
| 45 | + mockLog.AssertExpectations(t) |
| 46 | +} |
| 47 | + |
| 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 | +} |
0 commit comments