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

hazendaz / httpunit / 755

14 Feb 2026 07:14PM UTC coverage: 80.526%. Remained the same
755

push

github

hazendaz
[ci] Fix badge

3213 of 4105 branches covered (78.27%)

Branch coverage included in aggregate %.

8245 of 10124 relevant lines covered (81.44%)

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
 * SPDX-License-Identifier: MIT
3
 * See LICENSE file for details.
4
 *
5
 * Copyright 2000-2026 Russell Gold
6
 * Copyright 2021-2000 hazendaz
7
 */
8
package com.meterware.httpunit.protocol;
9

10
import java.io.File;
11
import java.io.IOException;
12
import java.io.InputStream;
13
import java.nio.file.Files;
14

15
/**
16
 * A description of a file to be uploaded as part of a form submission.
17
 **/
18
public class UploadFileSpec {
19

20
    /**
21
     * Creates a specification based on a File object. The content type will be guessed from the file extension.
22
     *
23
     * @param file
24
     *            the file
25
     */
26
    public UploadFileSpec(File file) {
1✔
27
        _file = file;
1✔
28
        guessContentType();
1✔
29
    }
1✔
30

31
    /**
32
     * Creates a specification based on a File object and with a specified content type.
33
     *
34
     * @param file
35
     *            the file
36
     * @param contentType
37
     *            the content type
38
     */
39
    public UploadFileSpec(File file, String contentType) {
1✔
40
        _file = file;
1✔
41
        _contentType = contentType;
1✔
42
    }
1✔
43

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

60
    /**
61
     * get the Inputstream - even if it has been closed previously.
62
     *
63
     * @return the inputstream for the current file
64
     *
65
     * @throws IOException
66
     *             Signals that an I/O exception has occurred.
67
     */
68
    public InputStream getInputStream() throws IOException {
69
        if (_inputStream == null) {
1✔
70
            _inputStream = Files.newInputStream(_file.toPath());
1✔
71
        }
72
        try {
73
            _inputStream.available();
1✔
74
        } catch (IOException ex) {
×
75
            _inputStream = Files.newInputStream(_file.toPath());
×
76
        }
1✔
77
        return _inputStream;
1✔
78
    }
79

80
    /**
81
     * Gets the file name.
82
     *
83
     * @return the file name
84
     */
85
    public String getFileName() {
86
        if (_fileName == null) {
1✔
87
            _fileName = _file.getAbsolutePath();
1✔
88
        }
89
        return _fileName;
1✔
90
    }
91

92
    /**
93
     * Returns the content type associated with this file upload specification.
94
     *
95
     * @return the content type
96
     */
97
    public String getContentType() {
98
        return _contentType;
1✔
99
    }
100

101
    /** The file. */
102
    private File _file;
103

104
    /** The input stream. */
105
    private InputStream _inputStream;
106

107
    /** The file name. */
108
    private String _fileName;
109

110
    /** The content type. */
111
    private String _contentType = "text/plain";
1✔
112

113
    /** the default content extensions. */
114
    private static String[][] CONTENT_EXTENSIONS = { { "text/plain", "txt", "text" }, { "text/html", "htm", "html" },
1✔
115
            { "image/gif", "gif" }, { "image/jpeg", "jpg", "jpeg" }, { "image/png", "png" },
116
            { "image/tiff", "tif", "tiff" }, { "application/pdf", "pdf" }, { "application/octet-stream", "zip" } };
117

118
    /**
119
     * Guess content type.
120
     */
121
    private void guessContentType() {
122
        String extension = getExtension(_file.getName());
1✔
123
        for (String[] element : CONTENT_EXTENSIONS) {
1!
124
            for (int j = 1; j < element.length; j++) {
1✔
125
                if (extension.equalsIgnoreCase(element[j])) {
1✔
126
                    _contentType = element[0];
1✔
127
                    return;
1✔
128
                }
129
            }
130
        }
131
    }
×
132

133
    /**
134
     * Gets the extension.
135
     *
136
     * @param fileName
137
     *            the file name
138
     *
139
     * @return the extension
140
     */
141
    private String getExtension(String fileName) {
142
        return fileName.substring(fileName.lastIndexOf('.') + 1);
1✔
143
    }
144
}
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