1 // Copyright 2023 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.net.truth;
6 
7 import static com.google.common.truth.Fact.simpleFact;
8 import static com.google.common.truth.Truth.assertAbout;
9 
10 import androidx.annotation.Nullable;
11 
12 import com.google.common.truth.FailureMetadata;
13 import com.google.common.truth.IntegerSubject;
14 import com.google.common.truth.IterableSubject;
15 import com.google.common.truth.LongSubject;
16 import com.google.common.truth.MapSubject;
17 import com.google.common.truth.StringSubject;
18 import com.google.common.truth.Subject;
19 
20 import org.chromium.net.UrlResponseInfo;
21 
22 /**
23  * A custom Truth subject for Cronet's {@link UrlResponseInfo}. This is used
24  * to assert on the info methods for custom error messages.
25  * Example usage:
26  * <pre>
27  * // to assert the info's url string is not empty.
28  * assertThat(info).hasUrlThat().isNotEmpty();
29  * </pre>
30  */
31 public class UrlResponseInfoSubject extends Subject {
32     private final UrlResponseInfo mActual;
33 
UrlResponseInfoSubject(FailureMetadata metadata, UrlResponseInfo subject)34     protected UrlResponseInfoSubject(FailureMetadata metadata, UrlResponseInfo subject) {
35         super(metadata, subject);
36         mActual = subject;
37     }
38 
assertThat(@ullable UrlResponseInfo info)39     public static UrlResponseInfoSubject assertThat(@Nullable UrlResponseInfo info) {
40         return assertAbout(responseInfos()).that(info);
41     }
42 
responseInfos()43     public static Subject.Factory<UrlResponseInfoSubject, UrlResponseInfo> responseInfos() {
44         return UrlResponseInfoSubject::new;
45     }
46 
47     // Test assertions
hasHttpStatusCodeThat()48     public IntegerSubject hasHttpStatusCodeThat() {
49         assertNotNull();
50         return check("getHttpStatusCode()").that(mActual.getHttpStatusCode());
51     }
52 
hasHttpStatusTextThat()53     public StringSubject hasHttpStatusTextThat() {
54         assertNotNull();
55         return check("getHttpStatusText()").that(mActual.getHttpStatusText());
56     }
57 
hasHeadersListThat()58     public IterableSubject hasHeadersListThat() {
59         assertNotNull();
60         return check("getAllHeadersAsList()").that(mActual.getAllHeadersAsList());
61     }
62 
hasHeadersThat()63     public MapSubject hasHeadersThat() {
64         assertNotNull();
65         return check("getAllHeaders()").that(mActual.getAllHeaders());
66     }
67 
hasNegotiatedProtocolThat()68     public StringSubject hasNegotiatedProtocolThat() {
69         assertNotNull();
70         return check("getNegotiatedProtocol()").that(mActual.getNegotiatedProtocol());
71     }
72 
hasProxyServerThat()73     public StringSubject hasProxyServerThat() {
74         assertNotNull();
75         return check("getProxyServer()").that(mActual.getProxyServer());
76     }
77 
hasReceivedByteCountThat()78     public LongSubject hasReceivedByteCountThat() {
79         assertNotNull();
80         return check("getReceivedByteCount()").that(mActual.getReceivedByteCount());
81     }
82 
hasUrlThat()83     public StringSubject hasUrlThat() {
84         assertNotNull();
85         return check("getUrl()").that(mActual.getUrl());
86     }
87 
hasUrlChainThat()88     public IterableSubject hasUrlChainThat() {
89         assertNotNull();
90         return check("getUrlChain()").that(mActual.getUrlChain());
91     }
92 
wasCached()93     public void wasCached() {
94         assertNotNull();
95         if (!mActual.wasCached()) {
96             failWithoutActual(simpleFact("responseInfo expected to be cached"));
97         }
98     }
99 
wasNotCached()100     public void wasNotCached() {
101         assertNotNull();
102         if (mActual.wasCached()) {
103             failWithoutActual(simpleFact("responseInfo expected not to be cached"));
104         }
105     }
106 
107     // Checks that the actual ResponseInfoSubject is not null before doing any other assertions.
108     // This was not done in the assertThat method because the user may still
109     // assertThat(info).isNull().
assertNotNull()110     private void assertNotNull() {
111         if (mActual == null) {
112             failWithoutActual(simpleFact("the responseInfo is null"));
113         }
114     }
115 }
116