Skip to content

Commit f46cda4

Browse files
committed
Add tests and more correct status code handling
1 parent bdc4d68 commit f46cda4

File tree

11 files changed

+100
-22
lines changed

11 files changed

+100
-22
lines changed

gitlfs-client/src/main/java/ru/bozaro/gitlfs/client/Client.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,10 @@ public class Client {
3737
private static final int MAX_RETRY_COUNT = 2;
3838
private static final int MAX_REDIRECT_COUNT = 5;
3939
@NotNull
40+
private static final int[] DEFAULT_HTTP_SUCCESS = new int[]{
41+
HttpStatus.SC_OK
42+
};
43+
@NotNull
4044
private final ObjectMapper mapper;
4145
@NotNull
4246
private final AuthProvider authProvider;
@@ -302,7 +306,17 @@ protected <T extends HttpMethod, R> R doRequest(@Nullable Link link, @NotNull Re
302306
++retryCount;
303307
continue;
304308
}
305-
return task.processResponse(mapper, request);
309+
int[] success = task.statusCodes();
310+
if (success == null) {
311+
success = DEFAULT_HTTP_SUCCESS;
312+
}
313+
for (int item : success) {
314+
if (request.getStatusCode() == item) {
315+
return task.processResponse(mapper, request);
316+
}
317+
}
318+
// Unexpected status code.
319+
throw new RequestException(request);
306320
}
307321
}
308322

gitlfs-client/src/main/java/ru/bozaro/gitlfs/client/internal/JsonPost.java

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
import com.fasterxml.jackson.core.JsonProcessingException;
44
import com.fasterxml.jackson.databind.ObjectMapper;
55
import org.apache.commons.httpclient.HttpMethod;
6-
import org.apache.commons.httpclient.HttpStatus;
76
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
87
import org.apache.commons.httpclient.methods.PostMethod;
98
import org.jetbrains.annotations.NotNull;
10-
import ru.bozaro.gitlfs.client.exceptions.RequestException;
9+
import org.jetbrains.annotations.Nullable;
1110

1211
import java.io.IOException;
1312

@@ -40,14 +39,15 @@ public HttpMethod createRequest(@NotNull ObjectMapper mapper, @NotNull String ur
4039
return method;
4140
}
4241

42+
@Nullable
43+
@Override
44+
public int[] statusCodes() {
45+
return null;
46+
}
47+
4348
@NotNull
4449
@Override
4550
public Res processResponse(@NotNull ObjectMapper mapper, @NotNull HttpMethod request) throws IOException {
46-
switch (request.getStatusCode()) {
47-
case HttpStatus.SC_OK:
48-
return mapper.readValue(request.getResponseBodyAsStream(), type);
49-
default:
50-
throw new RequestException(request);
51-
}
51+
return mapper.readValue(request.getResponseBodyAsStream(), type);
5252
}
5353
}

gitlfs-client/src/main/java/ru/bozaro/gitlfs/client/internal/MetaGet.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import org.apache.commons.httpclient.HttpStatus;
66
import org.apache.commons.httpclient.methods.GetMethod;
77
import org.jetbrains.annotations.NotNull;
8-
import ru.bozaro.gitlfs.client.exceptions.RequestException;
8+
import org.jetbrains.annotations.Nullable;
99
import ru.bozaro.gitlfs.common.data.ObjectRes;
1010

1111
import java.io.IOException;
@@ -27,6 +27,15 @@ public HttpMethod createRequest(@NotNull ObjectMapper mapper, @NotNull String ur
2727
return req;
2828
}
2929

30+
@Nullable
31+
@Override
32+
public int[] statusCodes() {
33+
return new int[]{
34+
HttpStatus.SC_OK,
35+
HttpStatus.SC_NOT_FOUND,
36+
};
37+
}
38+
3039
@Override
3140
public ObjectRes processResponse(@NotNull ObjectMapper mapper, @NotNull HttpMethod request) throws IOException {
3241
switch (request.getStatusCode()) {
@@ -35,7 +44,7 @@ public ObjectRes processResponse(@NotNull ObjectMapper mapper, @NotNull HttpMeth
3544
case HttpStatus.SC_NOT_FOUND:
3645
return null;
3746
default:
38-
throw new RequestException(request);
47+
throw new IllegalStateException();
3948
}
4049
}
4150
}

gitlfs-client/src/main/java/ru/bozaro/gitlfs/client/internal/MetaPost.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
88
import org.apache.commons.httpclient.methods.PostMethod;
99
import org.jetbrains.annotations.NotNull;
10-
import ru.bozaro.gitlfs.client.exceptions.RequestException;
10+
import org.jetbrains.annotations.Nullable;
1111
import ru.bozaro.gitlfs.common.data.Meta;
1212
import ru.bozaro.gitlfs.common.data.ObjectRes;
1313

@@ -39,6 +39,15 @@ public HttpMethod createRequest(@NotNull ObjectMapper mapper, @NotNull String ur
3939
return req;
4040
}
4141

42+
@Nullable
43+
@Override
44+
public int[] statusCodes() {
45+
return new int[]{
46+
HttpStatus.SC_OK,
47+
HttpStatus.SC_ACCEPTED,
48+
};
49+
}
50+
4251
@Override
4352
public ObjectRes processResponse(@NotNull ObjectMapper mapper, @NotNull HttpMethod request) throws IOException {
4453
switch (request.getStatusCode()) {
@@ -47,7 +56,7 @@ public ObjectRes processResponse(@NotNull ObjectMapper mapper, @NotNull HttpMeth
4756
case HttpStatus.SC_ACCEPTED:
4857
return mapper.readValue(request.getResponseBodyAsStream(), ObjectRes.class);
4958
default:
50-
throw new RequestException(request);
59+
throw new IllegalStateException();
5160
}
5261
}
5362
}

gitlfs-client/src/main/java/ru/bozaro/gitlfs/client/internal/ObjectGet.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.apache.commons.httpclient.HttpMethod;
55
import org.apache.commons.httpclient.methods.GetMethod;
66
import org.jetbrains.annotations.NotNull;
7+
import org.jetbrains.annotations.Nullable;
78

89
import java.io.IOException;
910
import java.io.InputStream;
@@ -20,6 +21,12 @@ public HttpMethod createRequest(@NotNull ObjectMapper mapper, @NotNull String ur
2021
return new GetMethod(url);
2122
}
2223

24+
@Nullable
25+
@Override
26+
public int[] statusCodes() {
27+
return null;
28+
}
29+
2330
@Override
2431
public InputStream processResponse(@NotNull ObjectMapper mapper, @NotNull HttpMethod request) throws IOException {
2532
return request.getResponseBodyAsStream();

gitlfs-client/src/main/java/ru/bozaro/gitlfs/client/internal/ObjectPut.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.apache.commons.httpclient.methods.PutMethod;
66
import org.apache.commons.httpclient.methods.RequestEntity;
77
import org.jetbrains.annotations.NotNull;
8+
import org.jetbrains.annotations.Nullable;
89
import ru.bozaro.gitlfs.client.io.StreamProvider;
910

1011
import java.io.IOException;
@@ -62,6 +63,12 @@ public String getContentType() {
6263
return req;
6364
}
6465

66+
@Nullable
67+
@Override
68+
public int[] statusCodes() {
69+
return null;
70+
}
71+
6572
@Override
6673
public Void processResponse(@NotNull ObjectMapper mapper, @NotNull HttpMethod request) throws IOException {
6774
return null;

gitlfs-client/src/main/java/ru/bozaro/gitlfs/client/internal/ObjectVerify.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,10 @@
22

33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import org.apache.commons.httpclient.HttpMethod;
5-
import org.apache.commons.httpclient.HttpStatus;
65
import org.apache.commons.httpclient.methods.ByteArrayRequestEntity;
76
import org.apache.commons.httpclient.methods.PostMethod;
87
import org.jetbrains.annotations.NotNull;
9-
import ru.bozaro.gitlfs.client.exceptions.RequestException;
8+
import org.jetbrains.annotations.Nullable;
109
import ru.bozaro.gitlfs.common.data.Meta;
1110

1211
import java.io.IOException;
@@ -37,14 +36,14 @@ public HttpMethod createRequest(@NotNull ObjectMapper mapper, @NotNull String ur
3736
return req;
3837
}
3938

39+
@Nullable
40+
@Override
41+
public int[] statusCodes() {
42+
return null;
43+
}
44+
4045
@Override
4146
public Void processResponse(@NotNull ObjectMapper mapper, @NotNull HttpMethod request) throws IOException {
42-
switch (request.getStatusCode()) {
43-
case HttpStatus.SC_OK:
44-
case HttpStatus.SC_ACCEPTED:
45-
return null;
46-
default:
47-
throw new RequestException(request);
48-
}
47+
return null;
4948
}
5049
}

gitlfs-client/src/main/java/ru/bozaro/gitlfs/client/internal/Request.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import com.fasterxml.jackson.databind.ObjectMapper;
44
import org.apache.commons.httpclient.HttpMethod;
55
import org.jetbrains.annotations.NotNull;
6+
import org.jetbrains.annotations.Nullable;
67

78
import java.io.IOException;
89

@@ -16,4 +17,12 @@ public interface Request<R> {
1617
HttpMethod createRequest(@NotNull ObjectMapper mapper, @NotNull String url) throws IOException;
1718

1819
R processResponse(@NotNull ObjectMapper mapper, @NotNull HttpMethod request) throws IOException;
20+
21+
/**
22+
* Success status codes.
23+
*
24+
* @return Success status codes.
25+
*/
26+
@Nullable
27+
int[] statusCodes();
1928
}

gitlfs-pointer/src/test/java/ru/bozaro/gitlfs/pointer/PointerTest.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,18 @@ public static Object[][] parseValidProvider() {
3636
.put("size", "12345")
3737
.build()
3838
},
39+
new Object[]{
40+
"pointer-valid-03.dat",
41+
ImmutableMap.builder()
42+
.put("version", "https://git-lfs.github.com/spec/v1")
43+
.put("object-name", " Foo")
44+
.put("object.id", "F1")
45+
.put("object0123456789", ":)")
46+
.put("oid", "sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393")
47+
.put("name", "Текст в UTF-8")
48+
.put("size", "12345")
49+
.build()
50+
},
3951
};
4052
}
4153

@@ -70,6 +82,7 @@ public static Object[][] parseInvalidProvider() {
7082
new Object[]{"pointer-invalid-07.dat", "Non utf-8"},
7183
new Object[]{"pointer-invalid-08.dat", "Size is not number"},
7284
new Object[]{"pointer-invalid-09.dat", "Duplicate line"},
85+
new Object[]{"pointer-invalid-10.dat", "Duplicate version"},
7386
};
7487
}
7588

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
version https://git-lfs.github.com/spec/v1
2+
oid sha256:4d7a214614ab2935c943f9e0ff69d22eadbb8f32b1258daaa5e2ca24d17e2393
3+
size 12345
4+
version https://git-lfs.github.com/spec/v1

0 commit comments

Comments
 (0)