Request.java

1
package com.renomad.minum.web;
2
3
import java.util.Objects;
4
5
/**
6
 * An implementation of {@link IRequest}.
7
 * Contains implementations for behaviors dealing with the request data.
8
 */
9
public final class Request implements IRequest {
10
11
    private final Headers headers;
12
    private final RequestLine requestLine;
13
    private Body body;
14
    private final String remoteRequester;
15
    private final ISocketWrapper socketWrapper;
16
    private final IBodyProcessor bodyProcessor;
17
    private boolean hasStartedReadingBody;
18
19
    /**
20
     * Constructor for a HTTP request.
21
     * @param remoteRequester This is the remote address making the request
22
     */
23
    public Request(Headers headers,
24
            RequestLine requestLine,
25
            String remoteRequester,
26
            ISocketWrapper socketWrapper,
27
            IBodyProcessor bodyProcessor,
28
            boolean hasStartedReadingBody
29
    ) {
30
        this.headers = headers;
31
        this.requestLine = requestLine;
32
        this.remoteRequester = remoteRequester;
33
        this.socketWrapper = socketWrapper;
34
        this.bodyProcessor = bodyProcessor;
35
        this.hasStartedReadingBody = hasStartedReadingBody;
36
    }
37
38
39
    @Override
40
    public Headers getHeaders() {
41 1 1. getHeaders : replaced return value with null for com/renomad/minum/web/Request::getHeaders → KILLED
        return headers;
42
    }
43
44
    @Override
45
    public RequestLine getRequestLine() {
46 1 1. getRequestLine : replaced return value with null for com/renomad/minum/web/Request::getRequestLine → KILLED
        return requestLine;
47
    }
48
49
    @Override
50
    public Body getBody() {
51 1 1. getBody : negated conditional → KILLED
        if (hasStartedReadingBody) {
52
            throw new WebServerException("The InputStream in Request has already been accessed for reading, preventing body extraction from stream." +
53
                    " If intending to use getBody(), use it exclusively");
54
        }
55 1 1. getBody : negated conditional → KILLED
        if (body == null) {
56
            body = bodyProcessor.extractData(socketWrapper.getInputStream(), headers);
57
        }
58 1 1. getBody : replaced return value with null for com/renomad/minum/web/Request::getBody → TIMED_OUT
        return body;
59
    }
60
61
    @Override
62
    public String getRemoteRequester() {
63 1 1. getRemoteRequester : replaced return value with "" for com/renomad/minum/web/Request::getRemoteRequester → KILLED
        return remoteRequester;
64
    }
65
66
    @Override
67
    public ISocketWrapper getSocketWrapper() {
68 1 1. getSocketWrapper : removed call to com/renomad/minum/web/Request::checkForExistingBody → KILLED
        checkForExistingBody();
69
        hasStartedReadingBody = true;
70 1 1. getSocketWrapper : replaced return value with null for com/renomad/minum/web/Request::getSocketWrapper → TIMED_OUT
        return socketWrapper;
71
    }
72
73
    @Override
74
    public boolean equals(Object o) {
75 2 1. equals : negated conditional → KILLED
2. equals : replaced boolean return with false for com/renomad/minum/web/Request::equals → KILLED
        if (this == o) return true;
76 3 1. equals : replaced boolean return with true for com/renomad/minum/web/Request::equals → TIMED_OUT
2. equals : negated conditional → KILLED
3. equals : negated conditional → KILLED
        if (o == null || getClass() != o.getClass()) return false;
77
        Request request = (Request) o;
78 8 1. equals : negated conditional → TIMED_OUT
2. equals : negated conditional → TIMED_OUT
3. equals : negated conditional → TIMED_OUT
4. equals : replaced boolean return with true for com/renomad/minum/web/Request::equals → KILLED
5. equals : negated conditional → KILLED
6. equals : negated conditional → KILLED
7. equals : negated conditional → KILLED
8. equals : negated conditional → KILLED
        return hasStartedReadingBody == request.hasStartedReadingBody && Objects.equals(headers, request.headers) && Objects.equals(requestLine, request.requestLine) && Objects.equals(body, request.body) && Objects.equals(remoteRequester, request.remoteRequester) && Objects.equals(socketWrapper, request.socketWrapper) && Objects.equals(bodyProcessor, request.bodyProcessor);
79
    }
80
81
    @Override
82
    public int hashCode() {
83 1 1. hashCode : replaced int return with 0 for com/renomad/minum/web/Request::hashCode → TIMED_OUT
        return Objects.hash(headers, requestLine, body, remoteRequester, socketWrapper, bodyProcessor, hasStartedReadingBody);
84
    }
85
86
    @Override
87
    public String toString() {
88 1 1. toString : replaced return value with "" for com/renomad/minum/web/Request::toString → KILLED
        return "Request{" +
89
                "headers=" + headers +
90
                ", requestLine=" + requestLine +
91
                ", body=" + body +
92
                ", remoteRequester='" + remoteRequester + '\'' +
93
                ", socketWrapper=" + socketWrapper +
94
                ", hasStartedReadingBody=" + hasStartedReadingBody +
95
                '}';
96
    }
97
98
    @Override
99
    public Iterable<UrlEncodedKeyValue> getUrlEncodedIterable() {
100 1 1. getUrlEncodedIterable : removed call to com/renomad/minum/web/Request::checkForExistingBody → KILLED
        checkForExistingBody();
101 1 1. getUrlEncodedIterable : negated conditional → KILLED
        if (!headers.contentType().contains("application/x-www-form-urlencoded")) {
102
            throw new WebServerException("This request was not sent with a content type of application/x-www-form-urlencoded.  The content type was: " + headers.contentType());
103
        }
104 1 1. getUrlEncodedIterable : replaced return value with Collections.emptyList for com/renomad/minum/web/Request::getUrlEncodedIterable → KILLED
        return bodyProcessor.getUrlEncodedDataIterable(getSocketWrapper().getInputStream(), getHeaders().contentLength());
105
    }
106
107
    /**
108
     * This method is for verifying that the body is null, and throwing an exception
109
     * if not.  Several of the methods in this class depend on the InputStream not
110
     * having been read already - if the body was read, then the InputStream is finished,
111
     * and any further reading would be incorrect.
112
     */
113
    private void checkForExistingBody() {
114 1 1. checkForExistingBody : negated conditional → KILLED
        if (body != null) {
115
            throw new WebServerException("Requesting this after getting the body with getBody() will result in incorrect behavior.  " +
116
                    "If you intend to work with the Request at this level, do not use getBody");
117
        }
118 1 1. checkForExistingBody : negated conditional → KILLED
        if (hasStartedReadingBody) {
119
            throw new WebServerException("The InputStream has begun processing elsewhere.  Results are invalid.");
120
        }
121
    }
122
123
    @Override
124
    public Iterable<StreamingMultipartPartition> getMultipartIterable() {
125 1 1. getMultipartIterable : removed call to com/renomad/minum/web/Request::checkForExistingBody → TIMED_OUT
        checkForExistingBody();
126
        String boundaryKey = "boundary=";
127
        String contentType = getHeaders().contentType();
128
        int indexOfBoundaryKey = contentType.indexOf(boundaryKey);
129
        String boundaryValue;
130 2 1. getMultipartIterable : changed conditional boundary → SURVIVED
2. getMultipartIterable : negated conditional → KILLED
        if (indexOfBoundaryKey > 0) {
131
            // grab all the text after the key to obtain the boundary value
132 1 1. getMultipartIterable : Replaced integer addition with subtraction → TIMED_OUT
            boundaryValue = contentType.substring(indexOfBoundaryKey + boundaryKey.length());
133
        } else {
134
            throw new BadRequestException("Did not find a valid boundary value for the multipart input. Header was: " + contentType);
135
        }
136
137 1 1. getMultipartIterable : negated conditional → KILLED
        if (boundaryValue.isBlank()) {
138
            throw new BadRequestException("Boundary value was blank. Header was: " + contentType);
139
        }
140
141 1 1. getMultipartIterable : replaced return value with Collections.emptyList for com/renomad/minum/web/Request::getMultipartIterable → TIMED_OUT
        return bodyProcessor.getMultiPartIterable(getSocketWrapper().getInputStream(), boundaryValue, getHeaders().contentLength());
142
    }
143
144
    @Override
145
    public boolean hasAccessedBody() {
146 3 1. hasAccessedBody : negated conditional → TIMED_OUT
2. hasAccessedBody : negated conditional → TIMED_OUT
3. hasAccessedBody : replaced boolean return with true for com/renomad/minum/web/Request::hasAccessedBody → KILLED
        return hasStartedReadingBody || body != null;
147
    }
148
149
    @Override
150
    public IBodyProcessor getBodyProcessor() {
151 1 1. getBodyProcessor : replaced return value with null for com/renomad/minum/web/Request::getBodyProcessor → TIMED_OUT
        return bodyProcessor;
152
    }
153
154
    @Override
155
    public boolean isHasStartedReadingBody() {
156 2 1. isHasStartedReadingBody : replaced boolean return with true for com/renomad/minum/web/Request::isHasStartedReadingBody → TIMED_OUT
2. isHasStartedReadingBody : replaced boolean return with false for com/renomad/minum/web/Request::isHasStartedReadingBody → KILLED
        return hasStartedReadingBody;
157
    }
158
}

Mutations

41

1.1
Location : getHeaders
Killed by : com.renomad.minum.web.RequestTests
replaced return value with null for com/renomad/minum/web/Request::getHeaders → KILLED

46

1.1
Location : getRequestLine
Killed by : com.renomad.minum.web.RequestTests
replaced return value with null for com/renomad/minum/web/Request::getRequestLine → KILLED

51

1.1
Location : getBody
Killed by : com.renomad.minum.web.RequestTests
negated conditional → KILLED

55

1.1
Location : getBody
Killed by : com.renomad.minum.web.RequestTests
negated conditional → KILLED

58

1.1
Location : getBody
Killed by : none
replaced return value with null for com/renomad/minum/web/Request::getBody → TIMED_OUT

63

1.1
Location : getRemoteRequester
Killed by : com.renomad.minum.web.RequestTests
replaced return value with "" for com/renomad/minum/web/Request::getRemoteRequester → KILLED

68

1.1
Location : getSocketWrapper
Killed by : com.renomad.minum.web.RequestTests
removed call to com/renomad/minum/web/Request::checkForExistingBody → KILLED

70

1.1
Location : getSocketWrapper
Killed by : none
replaced return value with null for com/renomad/minum/web/Request::getSocketWrapper → TIMED_OUT

75

1.1
Location : equals
Killed by : com.renomad.minum.web.RequestTests
negated conditional → KILLED

2.2
Location : equals
Killed by : com.renomad.minum.web.RequestTests
replaced boolean return with false for com/renomad/minum/web/Request::equals → KILLED

76

1.1
Location : equals
Killed by : com.renomad.minum.web.RequestTests
negated conditional → KILLED

2.2
Location : equals
Killed by : none
replaced boolean return with true for com/renomad/minum/web/Request::equals → TIMED_OUT

3.3
Location : equals
Killed by : com.renomad.minum.web.RequestTests
negated conditional → KILLED

78

1.1
Location : equals
Killed by : none
negated conditional → TIMED_OUT

2.2
Location : equals
Killed by : com.renomad.minum.web.RequestTests
replaced boolean return with true for com/renomad/minum/web/Request::equals → KILLED

3.3
Location : equals
Killed by : com.renomad.minum.web.RequestTests
negated conditional → KILLED

4.4
Location : equals
Killed by : none
negated conditional → TIMED_OUT

5.5
Location : equals
Killed by : none
negated conditional → TIMED_OUT

6.6
Location : equals
Killed by : com.renomad.minum.web.RequestTests
negated conditional → KILLED

7.7
Location : equals
Killed by : com.renomad.minum.web.RequestTests
negated conditional → KILLED

8.8
Location : equals
Killed by : com.renomad.minum.web.RequestTests
negated conditional → KILLED

83

1.1
Location : hashCode
Killed by : none
replaced int return with 0 for com/renomad/minum/web/Request::hashCode → TIMED_OUT

88

1.1
Location : toString
Killed by : com.renomad.minum.web.RequestTests
replaced return value with "" for com/renomad/minum/web/Request::toString → KILLED

100

1.1
Location : getUrlEncodedIterable
Killed by : com.renomad.minum.web.RequestTests
removed call to com/renomad/minum/web/Request::checkForExistingBody → KILLED

101

1.1
Location : getUrlEncodedIterable
Killed by : com.renomad.minum.web.RequestTests
negated conditional → KILLED

104

1.1
Location : getUrlEncodedIterable
Killed by : com.renomad.minum.web.RequestTests
replaced return value with Collections.emptyList for com/renomad/minum/web/Request::getUrlEncodedIterable → KILLED

114

1.1
Location : checkForExistingBody
Killed by : com.renomad.minum.web.RequestTests
negated conditional → KILLED

118

1.1
Location : checkForExistingBody
Killed by : com.renomad.minum.web.RequestTests
negated conditional → KILLED

125

1.1
Location : getMultipartIterable
Killed by : none
removed call to com/renomad/minum/web/Request::checkForExistingBody → TIMED_OUT

130

1.1
Location : getMultipartIterable
Killed by : com.renomad.minum.web.RequestTests
negated conditional → KILLED

2.2
Location : getMultipartIterable
Killed by : none
changed conditional boundary → SURVIVED
Covering tests

132

1.1
Location : getMultipartIterable
Killed by : none
Replaced integer addition with subtraction → TIMED_OUT

137

1.1
Location : getMultipartIterable
Killed by : com.renomad.minum.web.RequestTests
negated conditional → KILLED

141

1.1
Location : getMultipartIterable
Killed by : none
replaced return value with Collections.emptyList for com/renomad/minum/web/Request::getMultipartIterable → TIMED_OUT

146

1.1
Location : hasAccessedBody
Killed by : none
negated conditional → TIMED_OUT

2.2
Location : hasAccessedBody
Killed by : none
negated conditional → TIMED_OUT

3.3
Location : hasAccessedBody
Killed by : com.renomad.minum.FunctionalTests.test_PathFunction_Response_Range(com.renomad.minum.FunctionalTests)
replaced boolean return with true for com/renomad/minum/web/Request::hasAccessedBody → KILLED

151

1.1
Location : getBodyProcessor
Killed by : none
replaced return value with null for com/renomad/minum/web/Request::getBodyProcessor → TIMED_OUT

156

1.1
Location : isHasStartedReadingBody
Killed by : none
replaced boolean return with true for com/renomad/minum/web/Request::isHasStartedReadingBody → TIMED_OUT

2.2
Location : isHasStartedReadingBody
Killed by : com.renomad.minum.web.RequestTests
replaced boolean return with false for com/renomad/minum/web/Request::isHasStartedReadingBody → KILLED

Active mutators

Tests examined


Report generated by PIT 1.17.0