|
| 1 | +package attachment |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/textproto" |
| 5 | + "reflect" |
| 6 | + "testing" |
| 7 | +) |
| 8 | + |
| 9 | +func TestMakeHeader(t *testing.T) { |
| 10 | + tests := []struct { |
| 11 | + name string |
| 12 | + contentType string |
| 13 | + contentID string |
| 14 | + fileName string |
| 15 | + encoding string |
| 16 | + disposition string |
| 17 | + want textproto.MIMEHeader |
| 18 | + }{ |
| 19 | + { |
| 20 | + name: "inline attachment", |
| 21 | + contentType: "image/jpeg", |
| 22 | + contentID: "123", |
| 23 | + fileName: "test.jpg", |
| 24 | + encoding: "base64", |
| 25 | + disposition: "inline", |
| 26 | + want: textproto.MIMEHeader{ |
| 27 | + "Content-Disposition": []string{"inline"}, |
| 28 | + "Content-Id": []string{"<123>"}, |
| 29 | + "Content-Type": []string{"image/jpeg; name=\"test.jpg\""}, |
| 30 | + "Content-Transfer-Encoding": []string{"base64"}, |
| 31 | + }, |
| 32 | + }, |
| 33 | + { |
| 34 | + name: "regular attachment", |
| 35 | + contentType: "application/pdf", |
| 36 | + contentID: "", |
| 37 | + fileName: "doc.pdf", |
| 38 | + encoding: "base64", |
| 39 | + disposition: "attachment", |
| 40 | + want: textproto.MIMEHeader{ |
| 41 | + "Content-Disposition": []string{"attachment; filename=\"doc.pdf\""}, |
| 42 | + "Content-Type": []string{"application/pdf; name=\"doc.pdf\""}, |
| 43 | + "Content-Transfer-Encoding": []string{"base64"}, |
| 44 | + }, |
| 45 | + }, |
| 46 | + { |
| 47 | + name: "default values", |
| 48 | + contentType: "", |
| 49 | + contentID: "", |
| 50 | + fileName: "file.txt", |
| 51 | + encoding: "", |
| 52 | + disposition: "", |
| 53 | + want: textproto.MIMEHeader{ |
| 54 | + "Content-Disposition": []string{"attachment; filename=\"file.txt\""}, |
| 55 | + "Content-Type": []string{"application/octet-stream; name=\"file.txt\""}, |
| 56 | + "Content-Transfer-Encoding": []string{"base64"}, |
| 57 | + }, |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for _, tt := range tests { |
| 62 | + t.Run(tt.name, func(t *testing.T) { |
| 63 | + got := MakeHeader(tt.contentType, tt.contentID, tt.fileName, tt.encoding, tt.disposition) |
| 64 | + if !reflect.DeepEqual(got, tt.want) { |
| 65 | + t.Errorf("MakeHeader() = %v, want %v", got, tt.want) |
| 66 | + } |
| 67 | + }) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +func TestAttachments_Scan(t *testing.T) { |
| 72 | + tests := []struct { |
| 73 | + name string |
| 74 | + input interface{} |
| 75 | + want Attachments |
| 76 | + wantErr bool |
| 77 | + }{ |
| 78 | + { |
| 79 | + name: "nil input", |
| 80 | + input: nil, |
| 81 | + want: Attachments{}, |
| 82 | + wantErr: false, |
| 83 | + }, |
| 84 | + { |
| 85 | + name: "valid json", |
| 86 | + input: []byte(`[{"name":"test.jpg","size":1024,"content_type":"image/jpeg","disposition":"attachment"}]`), |
| 87 | + want: Attachments{ |
| 88 | + { |
| 89 | + Name: "test.jpg", |
| 90 | + Size: 1024, |
| 91 | + ContentType: "image/jpeg", |
| 92 | + Disposition: "attachment", |
| 93 | + }, |
| 94 | + }, |
| 95 | + wantErr: false, |
| 96 | + }, |
| 97 | + { |
| 98 | + name: "invalid type", |
| 99 | + input: "not bytes", |
| 100 | + want: nil, |
| 101 | + wantErr: true, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tt := range tests { |
| 106 | + t.Run(tt.name, func(t *testing.T) { |
| 107 | + var a Attachments |
| 108 | + err := a.Scan(tt.input) |
| 109 | + if (err != nil) != tt.wantErr { |
| 110 | + t.Errorf("Attachments.Scan() error = %v, wantErr %v", err, tt.wantErr) |
| 111 | + return |
| 112 | + } |
| 113 | + if !tt.wantErr && !reflect.DeepEqual(a, tt.want) { |
| 114 | + t.Errorf("Attachments.Scan() = %v, want %v", a, tt.want) |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
0 commit comments