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

hazendaz / httpunit / 656

06 Dec 2025 09:11PM UTC coverage: 80.452% (+0.02%) from 80.435%
656

push

github

hazendaz
[maven-release-plugin] prepare for next development iteration

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10137 relevant lines covered (81.34%)

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.IOException;
24
import java.io.InputStream;
25
import java.nio.file.Files;
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
     * @param file
36
     *            the file
37
     */
38
    public UploadFileSpec(File file) {
1✔
39
        _file = file;
1✔
40
        guessContentType();
1✔
41
    }
1✔
42

43
    /**
44
     * Creates a specification based on a File object and with a specified content type.
45
     *
46
     * @param file
47
     *            the file
48
     * @param contentType
49
     *            the content type
50
     */
51
    public UploadFileSpec(File file, String contentType) {
1✔
52
        _file = file;
1✔
53
        _contentType = contentType;
1✔
54
    }
1✔
55

56
    /**
57
     * Creates a specification for an upload from an input stream. The file name and content type must be specified.
58
     *
59
     * @param fileName
60
     *            the file name
61
     * @param inputStream
62
     *            the input stream
63
     * @param contentType
64
     *            the content type
65
     */
66
    public UploadFileSpec(String fileName, InputStream inputStream, String contentType) {
1✔
67
        _fileName = fileName;
1✔
68
        _inputStream = inputStream;
1✔
69
        _contentType = contentType;
1✔
70
    }
1✔
71

72
    /**
73
     * get the Inputstream - even if it has been closed previously.
74
     *
75
     * @return the inputstream for the current file
76
     *
77
     * @throws IOException
78
     *             Signals that an I/O exception has occurred.
79
     */
80
    public InputStream getInputStream() throws IOException {
81
        if (_inputStream == null) {
1✔
82
            _inputStream = Files.newInputStream(_file.toPath());
1✔
83
        }
84
        try {
85
            _inputStream.available();
1✔
86
        } catch (IOException ex) {
×
87
            _inputStream = Files.newInputStream(_file.toPath());
×
88
        }
1✔
89
        return _inputStream;
1✔
90
    }
91

92
    /**
93
     * Gets the file name.
94
     *
95
     * @return the file name
96
     */
97
    public String getFileName() {
98
        if (_fileName == null) {
1✔
99
            _fileName = _file.getAbsolutePath();
1✔
100
        }
101
        return _fileName;
1✔
102
    }
103

104
    /**
105
     * Returns the content type associated with this file upload specification.
106
     *
107
     * @return the content type
108
     */
109
    public String getContentType() {
110
        return _contentType;
1✔
111
    }
112

113
    /** The file. */
114
    private File _file;
115

116
    /** The input stream. */
117
    private InputStream _inputStream;
118

119
    /** The file name. */
120
    private String _fileName;
121

122
    /** The content type. */
123
    private String _contentType = "text/plain";
1✔
124

125
    /** the default content extensions. */
126
    private static String[][] CONTENT_EXTENSIONS = { { "text/plain", "txt", "text" }, { "text/html", "htm", "html" },
1✔
127
            { "image/gif", "gif" }, { "image/jpeg", "jpg", "jpeg" }, { "image/png", "png" },
128
            { "image/tiff", "tif", "tiff" }, { "application/pdf", "pdf" }, { "application/octet-stream", "zip" } };
129

130
    /**
131
     * Guess content type.
132
     */
133
    private void guessContentType() {
134
        String extension = getExtension(_file.getName());
1✔
135
        for (String[] element : CONTENT_EXTENSIONS) {
1!
136
            for (int j = 1; j < element.length; j++) {
1✔
137
                if (extension.equalsIgnoreCase(element[j])) {
1✔
138
                    _contentType = element[0];
1✔
139
                    return;
1✔
140
                }
141
            }
142
        }
143
    }
×
144

145
    /**
146
     * Gets the extension.
147
     *
148
     * @param fileName
149
     *            the file name
150
     *
151
     * @return the extension
152
     */
153
    private String getExtension(String fileName) {
154
        return fileName.substring(fileName.lastIndexOf('.') + 1);
1✔
155
    }
156
}
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