-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathCommonsFileUploadAppSecModuleTest.groovy
More file actions
64 lines (50 loc) · 1.78 KB
/
CommonsFileUploadAppSecModuleTest.groovy
File metadata and controls
64 lines (50 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import datadog.trace.instrumentation.commons.fileupload.FileItemContentReader
import org.apache.commons.fileupload.FileItem
import spock.lang.Specification
class CommonsFileUploadAppSecModuleTest extends Specification {
def "readContent returns full content when smaller than limit"() {
given:
def content = 'Hello, World!'
def item = fileItem(content)
expect:
FileItemContentReader.readContent(item) == content
}
def "readContent truncates content to MAX_CONTENT_BYTES"() {
given:
def largeContent = 'X' * (FileItemContentReader.MAX_CONTENT_BYTES + 500)
def item = fileItem(largeContent)
when:
def result = FileItemContentReader.readContent(item)
then:
result.length() == FileItemContentReader.MAX_CONTENT_BYTES
result == 'X' * FileItemContentReader.MAX_CONTENT_BYTES
}
def "readContent returns empty string when getInputStream throws"() {
given:
FileItem item = Stub(FileItem)
item.getInputStream() >> { throw new IOException('simulated error') }
expect:
FileItemContentReader.readContent(item) == ''
}
def "readContent returns empty string for empty content"() {
given:
def item = fileItem('')
expect:
FileItemContentReader.readContent(item) == ''
}
def "readContent reads exactly MAX_CONTENT_BYTES when content equals the limit"() {
given:
def content = 'A' * FileItemContentReader.MAX_CONTENT_BYTES
def item = fileItem(content)
when:
def result = FileItemContentReader.readContent(item)
then:
result.length() == FileItemContentReader.MAX_CONTENT_BYTES
result == content
}
private FileItem fileItem(String content) {
FileItem item = Stub(FileItem)
item.getInputStream() >> new ByteArrayInputStream(content.getBytes('ISO-8859-1'))
return item
}
}