项目作者: altfatterz

项目描述 :
file upload example
高级语言: Java
项目地址: git://github.com/altfatterz/invoice-service.git
创建时间: 2019-09-13T16:17:34Z
项目社区:https://github.com/altfatterz/invoice-service

开源协议:

下载


Uploading files using a demo invoice-service

Upload success

Invoice upload test with pdf files

  1. $ http -v -f :8080/invoices files@./samples/invoice1.pdf files@./samples/invoice2.pdf
  2. POST /invoices HTTP/1.1
  3. Accept: */*
  4. Accept-Encoding: gzip, deflate
  5. Connection: keep-alive
  6. Content-Length: 16520
  7. Content-Type: multipart/form-data; boundary=8a19fde949172660a36f7bf79285027c
  8. Host: localhost:8080
  9. User-Agent: HTTPie/1.0.2
  10. +-----------------------------------------+
  11. | NOTE: binary data not shown in terminal |
  12. +-----------------------------------------+
  13. HTTP/1.1 200
  14. Content-Length: 0
  15. Date: Fri, 13 Sep 2019 13:03:17 GMT

Invoice upload test with images files

  1. $ http -v -f :8080/invoices files@./samples/invoice3.jpg
  2. POST /invoices HTTP/1.1
  3. Accept: */*
  4. Accept-Encoding: gzip, deflate
  5. Connection: keep-alive
  6. Content-Length: 51260
  7. Content-Type: multipart/form-data; boundary=702c2e570f95b7752e9022ea43be8326
  8. Host: localhost:8080
  9. User-Agent: HTTPie/1.0.2
  10. +-----------------------------------------+
  11. | NOTE: binary data not shown in terminal |
  12. +-----------------------------------------+
  13. HTTP/1.1 200
  14. Content-Length: 0
  15. Date: Fri, 13 Sep 2019 20:10:40 GMT

Upload failed

Not supported mime type

  1. $ http -v -f :8080/invoices files@./samples/invoice4.txt
  2. POST /invoices HTTP/1.1
  3. Accept: */*
  4. Accept-Encoding: gzip, deflate
  5. Connection: keep-alive
  6. Content-Length: 190
  7. Content-Type: multipart/form-data; boundary=45b35d67bd487b6f302672969191e54b
  8. Host: localhost:8080
  9. User-Agent: HTTPie/1.0.2
  10. --45b35d67bd487b6f302672969191e54b
  11. Content-Disposition: form-data; name="files"; filename="invoice4.txt"
  12. Content-Type: text/plain
  13. payment is late
  14. --45b35d67bd487b6f302672969191e54b--
  15. HTTP/1.1 400
  16. Connection: close
  17. Content-Type: application/json;charset=UTF-8
  18. Date: Fri, 13 Sep 2019 20:12:02 GMT
  19. Transfer-Encoding: chunked
  20. {
  21. "error": "Bad Request",
  22. "message": "only [image/jpeg, image/jpg, application/pdf] are allowed",
  23. "path": "/invoices",
  24. "status": 400,
  25. "timestamp": "2019-09-13T20:12:02.290+0000"
  26. }

Not valid content

  1. http -v -f :8080/invoices files@./samples/invoice6.jpg
  2. POST /invoices HTTP/1.1
  3. Accept: */*
  4. Accept-Encoding: gzip, deflate
  5. Connection: keep-alive
  6. Content-Length: 8274
  7. Content-Type: multipart/form-data; boundary=febfd9089a44ea85d2d4f5c5fb1d10d8
  8. Host: localhost:8080
  9. User-Agent: HTTPie/1.0.2
  10. +-----------------------------------------+
  11. | NOTE: binary data not shown in terminal |
  12. +-----------------------------------------+
  13. HTTP/1.1 400
  14. Connection: close
  15. Content-Type: application/json;charset=UTF-8
  16. Date: Fri, 13 Sep 2019 20:13:37 GMT
  17. Transfer-Encoding: chunked
  18. {
  19. "error": "Bad Request",
  20. "message": "the file content is not in sync with the file extension",
  21. "path": "/invoices",
  22. "status": 400,
  23. "timestamp": "2019-09-13T20:13:37.255+0000"
  24. }

Content detection using Apache Tika

  1. <dependency>
  2. <groupId>org.apache.tika</groupId>
  3. <artifactId>tika-core</artifactId>
  4. <version>1.22</version>
  5. </dependency>

Tika detects the media type of the given document. The type detection is based on the content of the given document stream.

  1. Tika tika = new Tika();
  2. String detectedContentType = tika.detect(multipartFile.getInputStream());

Control upload size

  1. spring:
  2. servlet:
  3. multipart:
  4. max-file-size: 1MB
  5. max-request-size: 10MB

Spring Cloud Contract

Generate the tests

  1. $ mvn clean spring-cloud-contract:generateTests

The InvoicesTest is generated

  1. public class InvoicesTest extends ContractBaseClass {
  2. @Test
  3. public void validate_upload_invoices_failed() throws Exception {
  4. // given:
  5. MockMvcRequestSpecification request = given()
  6. .header("Content-Type", "multipart/form-data")
  7. .multiPart("files", "invoice.jpg", new byte[] {37, 80, 68, 70, 45}, "image/jpg");
  8. // when:
  9. ResponseOptions response = given().spec(request)
  10. .post("/invoices");
  11. // then:
  12. assertThat(response.statusCode()).isEqualTo(400);
  13. }
  14. @Test
  15. public void validate_upload_invoices_success() throws Exception {
  16. // given:
  17. MockMvcRequestSpecification request = given()
  18. .header("Content-Type", "multipart/form-data")
  19. .multiPart("files", "invoice.pdf", new byte[] {37, 80, 68, 70, 45, 49, 46}, "application/pdf");
  20. // when:
  21. ResponseOptions response = given().spec(request)
  22. .post("/invoices");
  23. // then:
  24. assertThat(response.statusCode()).isEqualTo(200);
  25. }
  26. }

Stubrunner

Start up the stubrunner via Spring Boot Cloud CLI

  1. $ sdk install springboot 2.1.8.RELEASE
  2. $ spring install org.springframework.cloud:spring-cloud-cli:2.1.0.RELEASE
  3. $ spring cloud stubrunner

The command is using the stubrunner.yml

  1. stubrunner:
  2. stubsMode: LOCAL
  3. ids:
  4. - com.example:invoice-service:+:9876

To see the registered stubs:

  1. $ http :8750/stubs
  2. HTTP/1.1 200
  3. Content-Type: application/json;charset=UTF-8
  4. Date: Fri, 13 Sep 2019 20:00:02 GMT
  5. Transfer-Encoding: chunked
  6. {
  7. "com.example:invoice-service:0.0.1-SNAPSHOT:stubs": 9876
  8. }

The invoice-service stub is running on the 9876 port

  1. $ http -v -f :9876/invoices files@./samples/invoice1.pdf files@./samples/invoice2.pdf
  2. POST /invoices HTTP/1.1
  3. Accept: */*
  4. Accept-Encoding: gzip, deflate
  5. Connection: keep-alive
  6. Content-Length: 16520
  7. Content-Type: multipart/form-data; boundary=ff4bb10bb5deb38ab89910a95bdd4910
  8. Host: localhost:9876
  9. User-Agent: HTTPie/1.0.2
  10. +-----------------------------------------+
  11. | NOTE: binary data not shown in terminal |
  12. +-----------------------------------------+
  13. HTTP/1.1 200 OK
  14. Content-Encoding: gzip
  15. Matched-Stub-Id: 2809a14f-34bb-41db-9fd2-f16b4fefcfad
  16. Server: Jetty(9.2.z-SNAPSHOT)
  17. Transfer-Encoding: chunked
  18. Vary: Accept-Encoding, User-Agent