Skip to content

Commit 6b900ae

Browse files
Add tests for HEAD, PUT, PATCH, and DELETE methods (#36)
1 parent d1f5d75 commit 6b900ae

File tree

4 files changed

+215
-0
lines changed

4 files changed

+215
-0
lines changed

test/test_delete.f90

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
program test_delete
2+
use iso_fortran_env, only: stderr => error_unit
3+
use http, only: request, pair_type, HTTP_DELETE, response_type
4+
implicit none
5+
type(response_type) :: res
6+
character(:), allocatable :: original_content, msg
7+
logical :: ok = .true.
8+
9+
original_content = '{"id":1,"title":"iPhone 9","description":"An apple mobile which is nothing like &
10+
apple","price":549,"discountPercentage":12.96,"rating":4.69,"stock":94,"brand":"Apple","category":&
11+
"smartphones","thumbnail":"https://i.dummyjson.com/data/products/1/thumbnail.jpg","images":&
12+
["https://i.dummyjson.com/data/products/1/1.jpg","https://i.dummyjson.com/data/products/1/2.jpg",&
13+
"https://i.dummyjson.com/data/products/1/3.jpg","https://i.dummyjson.com/data/products/1/4.jpg",&
14+
"https://i.dummyjson.com/data/products/1/thumbnail.jpg"],"isDeleted":true'
15+
16+
res = request(url='https://dummyjson.com/products/1', method=HTTP_DELETE)
17+
18+
msg = 'test_delete: '
19+
20+
if (.not. res%ok) then
21+
ok = .false.
22+
msg = msg // res%err_msg
23+
write(stderr, '(a)') msg
24+
error stop 1
25+
end if
26+
27+
! Status Code Validation
28+
if (res%status_code /= 200) then
29+
ok = .false.
30+
print '(a)', 'Failed : Status Code Validation'
31+
end if
32+
33+
! Content Length Validation
34+
if (res%content_length /= (len(original_content)+40) .or. &
35+
len(res%content) /= (len(original_content)+40)) then
36+
ok = .false.
37+
print '(a)', 'Failed : Content Length Validation'
38+
end if
39+
40+
! Content Validation
41+
if (res%content(1:535) /= original_content) then
42+
ok = .false.
43+
print '(a)', 'Failed : Content Validation'
44+
end if
45+
46+
if (.not. ok) then
47+
msg = msg // 'Test Case Failed'
48+
write(stderr, '(a)'), msg
49+
error stop 1
50+
else
51+
msg = msg // 'All tests passed.'
52+
print '(a)', msg
53+
end if
54+
55+
end program test_delete

test/test_head.f90

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
program test_head
2+
use iso_fortran_env, only: stderr => error_unit
3+
use http, only : response_type, request, HTTP_HEAD
4+
5+
implicit none
6+
type(response_type) :: res
7+
character(:), allocatable :: msg
8+
logical :: ok = .true.
9+
10+
11+
res = request(url='https://www.w3schools.com/python/demopage.php', method=HTTP_HEAD)
12+
13+
msg = 'test_head: '
14+
15+
if (.not. res%ok) then
16+
ok = .false.
17+
msg = msg // res%err_msg
18+
write(stderr, '(a)') msg
19+
error stop 1
20+
end if
21+
22+
! Status Code Validation
23+
if (res%status_code /= 200) then
24+
ok = .false.
25+
print '(a)', 'Failed : Status Code Validation'
26+
end if
27+
28+
! Header Size Validation
29+
if (size(res%header) /= 13) then
30+
ok = .false.
31+
print '(a)', 'Failed : Header Size Validation'
32+
end if
33+
34+
if (.not. ok) then
35+
msg = msg // 'Test Case Failed'
36+
write(stderr, '(a)'), msg
37+
error stop 1
38+
else
39+
msg = msg // 'All tests passed.'
40+
print '(a)', msg
41+
end if
42+
end program test_head

test/test_patch.f90

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
program test_patch
2+
use iso_fortran_env, only: stderr => error_unit
3+
use http, only: request, pair_type, HTTP_PATCH, response_type
4+
implicit none
5+
type(response_type) :: res
6+
character(:), allocatable :: json_data, original_content, msg
7+
type(pair_type), allocatable :: req_header(:)
8+
logical :: ok = .true.
9+
10+
original_content = '{"id":1,"title":"iPhone Galaxy +1","price":549,"stock":94,"rating":4.69,&
11+
"images":["https://i.dummyjson.com/data/products/1/1.jpg","https://i.dummyjson.com/data/products/1/2.jpg"&
12+
,"https://i.dummyjson.com/data/products/1/3.jpg","https://i.dummyjson.com/data/products/1/4.jpg",&
13+
"https://i.dummyjson.com/data/products/1/thumbnail.jpg"],"thumbnail":"https://i.dummyjson.com/data/products/1/thumbnail.jpg",&
14+
"description":"An apple mobile which is nothing like apple","brand":"Apple","category":"smartphones"}'
15+
16+
req_header = [pair_type('Content-Type', 'application/json')]
17+
18+
json_data = '{"title":"iPhone Galaxy +1"}'
19+
20+
res = request(url='https://dummyjson.com/products/1', method=HTTP_PATCH, data=json_data, header=req_header)
21+
22+
msg = 'test_patch: '
23+
24+
if (.not. res%ok) then
25+
ok = .false.
26+
msg = msg // res%err_msg
27+
write(stderr, '(a)') msg
28+
error stop 1
29+
end if
30+
31+
! Status Code Validation
32+
if (res%status_code /= 200) then
33+
ok = .false.
34+
print '(a)', 'Failed : Status Code Validation'
35+
end if
36+
37+
! Content Length Validation
38+
if (res%content_length /= len(original_content) .or. &
39+
len(res%content) /= len(original_content)) then
40+
ok = .false.
41+
print '(a)', 'Failed : Content Length Validation'
42+
end if
43+
44+
! Content Validation
45+
if (res%content /= original_content) then
46+
ok = .false.
47+
print '(a)', 'Failed : Content Validation'
48+
end if
49+
50+
if (.not. ok) then
51+
msg = msg // 'Test Case Failed'
52+
write(stderr, '(a)'), msg
53+
error stop 1
54+
else
55+
msg = msg // 'All tests passed.'
56+
print '(a)', msg
57+
end if
58+
59+
end program test_patch

test/test_put.f90

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
program test_put
2+
use iso_fortran_env, only: stderr => error_unit
3+
use http, only: request, pair_type, HTTP_PUT, response_type
4+
implicit none
5+
type(response_type) :: res
6+
character(:), allocatable :: json_data, original_content, msg
7+
type(pair_type), allocatable :: req_header(:)
8+
logical :: ok = .true.
9+
10+
original_content = '{"id":1,"title":"iPhone Galaxy +1","price":549,"stock":94,"rating":4.69,&
11+
"images":["https://i.dummyjson.com/data/products/1/1.jpg","https://i.dummyjson.com/data/products/1/2.jpg"&
12+
,"https://i.dummyjson.com/data/products/1/3.jpg","https://i.dummyjson.com/data/products/1/4.jpg",&
13+
"https://i.dummyjson.com/data/products/1/thumbnail.jpg"],"thumbnail":"https://i.dummyjson.com/data/products/1/thumbnail.jpg",&
14+
"description":"An apple mobile which is nothing like apple","brand":"Apple","category":"smartphones"}'
15+
16+
req_header = [pair_type('Content-Type', 'application/json')]
17+
18+
json_data = '{"title":"iPhone Galaxy +1"}'
19+
20+
res = request(url='https://dummyjson.com/products/1', method=HTTP_PUT, data=json_data, header=req_header)
21+
22+
msg = 'test_put: '
23+
24+
if (.not. res%ok) then
25+
ok = .false.
26+
msg = msg // res%err_msg
27+
write(stderr, '(a)') msg
28+
error stop 1
29+
end if
30+
31+
! Status Code Validation
32+
if (res%status_code /= 200) then
33+
ok = .false.
34+
print '(a)', 'Failed : Status Code Validation'
35+
end if
36+
37+
! Content Length Validation
38+
if (res%content_length /= len(original_content) .or. &
39+
len(res%content) /= len(original_content)) then
40+
ok = .false.
41+
print '(a)', 'Failed : Content Length Validation'
42+
end if
43+
44+
! Content Validation
45+
if (res%content /= original_content) then
46+
ok = .false.
47+
print '(a)', 'Failed : Content Validation'
48+
end if
49+
50+
if (.not. ok) then
51+
msg = msg // 'Test Case Failed'
52+
write(stderr, '(a)'), msg
53+
error stop 1
54+
else
55+
msg = msg // 'All tests passed.'
56+
print '(a)', msg
57+
end if
58+
59+
end program test_put

0 commit comments

Comments
 (0)