• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

hazendaz / httpunit / 636

05 Dec 2025 03:27AM UTC coverage: 80.509%. Remained the same
636

push

github

hazendaz
Cleanup more old since tags

you guessed it, at this point going to jautodoc the rest so the warnings on builds go away ;)

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8249 of 10132 relevant lines covered (81.42%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

90.91
/src/main/java/com/meterware/httpunit/protocol/UploadFileSpec.java
1
/*
2
 * MIT License
3
 *
4
 * Copyright 2011-2025 Russell Gold
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7
 * documentation files (the "Software"), to deal in the Software without restriction, including without limitation
8
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
9
 * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in all copies or substantial portions
12
 * of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
15
 * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
17
 * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18
 * DEALINGS IN THE SOFTWARE.
19
 */
20
package com.meterware.httpunit.protocol;
21

22
import java.io.File;
23
import java.io.FileInputStream;
24
import java.io.IOException;
25
import java.io.InputStream;
26

27
/**
28
 * A description of a file to be uploaded as part of a form submission.
29
 **/
30
public class UploadFileSpec {
31

32
    /**
33
     * Creates a specification based on a File object. The content type will be guessed from the file extension.
34
     */
35
    public UploadFileSpec(File file) {
1✔
36
        _file = file;
1✔
37
        guessContentType();
1✔
38
    }
1✔
39

40
    /**
41
     * Creates a specification based on a File object and with a specified content type.
42
     */
43
    public UploadFileSpec(File file, String contentType) {
1✔
44
        _file = file;
1✔
45
        _contentType = contentType;
1✔
46
    }
1✔
47

48
    /**
49
     * Creates a specification for an upload from an input stream. The file name and content type must be specified.
50
     */
51
    public UploadFileSpec(String fileName, InputStream inputStream, String contentType) {
1✔
52
        _fileName = fileName;
1✔
53
        _inputStream = inputStream;
1✔
54
        _contentType = contentType;
1✔
55
    }
1✔
56

57
    /**
58
     * get the Inputstream - even if it has been closed previously
59
     *
60
     * @return the inputstream for the current file
61
     *
62
     * @throws IOException
63
     */
64
    public InputStream getInputStream() throws IOException {
65
        if (_inputStream == null) {
1✔
66
            _inputStream = new FileInputStream(_file);
1✔
67
        }
68
        try {
69
            _inputStream.available();
1✔
70
        } catch (IOException ex) {
×
71
            _inputStream = new FileInputStream(_file);
×
72
        }
1✔
73
        return _inputStream;
1✔
74
    }
75

76
    public String getFileName() {
77
        if (_fileName == null) {
1✔
78
            _fileName = _file.getAbsolutePath();
1✔
79
        }
80
        return _fileName;
1✔
81
    }
82

83
    /**
84
     * Returns the content type associated with this file upload specification.
85
     */
86
    public String getContentType() {
87
        return _contentType;
1✔
88
    }
89

90
    private File _file;
91

92
    private InputStream _inputStream;
93

94
    private String _fileName;
95

96
    private String _contentType = "text/plain";
1✔
97

98
    /**
99
     * the default content extensions
100
     */
101
    private static String[][] CONTENT_EXTENSIONS = { { "text/plain", "txt", "text" }, { "text/html", "htm", "html" },
1✔
102
            { "image/gif", "gif" }, { "image/jpeg", "jpg", "jpeg" }, { "image/png", "png" },
103
            { "image/tiff", "tif", "tiff" }, { "application/pdf", "pdf" }, { "application/octet-stream", "zip" } };
104

105
    private void guessContentType() {
106
        String extension = getExtension(_file.getName());
1✔
107
        for (String[] element : CONTENT_EXTENSIONS) {
1!
108
            for (int j = 1; j < element.length; j++) {
1✔
109
                if (extension.equalsIgnoreCase(element[j])) {
1✔
110
                    _contentType = element[0];
1✔
111
                    return;
1✔
112
                }
113
            }
114
        }
115
    }
×
116

117
    private String getExtension(String fileName) {
118
        return fileName.substring(fileName.lastIndexOf('.') + 1);
1✔
119
    }
120
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc