summaryrefslogtreecommitdiff
path: root/test/etap/140-attachment-comp.t
blob: 98d37abc052a1861f27a7c7701b1241c6215a1f2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
#!/usr/bin/env escript
%% -*- erlang -*-

% Licensed under the Apache License, Version 2.0 (the "License"); you may not
% use this file except in compliance with the License. You may obtain a copy of
% the License at
%
%   http://www.apache.org/licenses/LICENSE-2.0
%
% Unless required by applicable law or agreed to in writing, software
% distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
% WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
% License for the specific language governing permissions and limitations under
% the License.

default_config() ->
    test_util:build_file("etc/couchdb/default_dev.ini").

test_db_name() ->
    <<"couch_test_atts_compression">>.

main(_) ->
    test_util:init_code_path(),

    etap:plan(78),
    case (catch test()) of
        ok ->
            etap:end_tests();
        Other ->
            etap:diag(io_lib:format("Test died abnormally: ~p", [Other])),
            etap:bail(Other)
    end,
    ok.

test() ->
    couch_server_sup:start_link([default_config()]),
    put(addr, couch_config:get("httpd", "bind_address", "127.0.0.1")),
    put(port, couch_config:get("httpd", "port", "5984")),
    application:start(inets),
    timer:sleep(1000),
    couch_server:delete(test_db_name(), []),
    couch_db:create(test_db_name(), []),

    couch_config:set("attachments", "compression_level", "8"),
    couch_config:set("attachments", "compressible_types", "text/*"),

    create_1st_text_att(),
    create_1st_png_att(),
    create_2nd_text_att(),
    create_2nd_png_att(),

    tests_for_1st_text_att(),
    tests_for_1st_png_att(),
    tests_for_2nd_text_att(),
    tests_for_2nd_png_att(),

    create_already_compressed_att(db_url() ++ "/doc_comp_att", "readme.txt"),
    test_already_compressed_att(db_url() ++ "/doc_comp_att", "readme.txt"),

    test_create_already_compressed_att_with_invalid_content_encoding(
        db_url() ++ "/doc_att_deflate",
        "readme.txt",
        zlib:compress(test_text_data()),
        "deflate"
    ),

    test_create_already_compressed_att_with_invalid_content_encoding(
        db_url() ++ "/doc_att_compress",
        "readme.txt",
        % Note: As of OTP R13B04, it seems there's no LZW compression
        % (i.e. UNIX compress utility implementation) lib in OTP.
        % However there's a simple working Erlang implementation at:
        % http://scienceblogs.com/goodmath/2008/01/simple_lempelziv_compression_i.php
        test_text_data(),
        "compress"
    ),

    timer:sleep(3000), % to avoid mochiweb socket closed exceptions
    couch_server:delete(test_db_name(), []),
    couch_server_sup:stop(),
    ok.

db_url() ->
    "http://" ++ get(addr) ++ ":" ++ get(port) ++ "/" ++
    binary_to_list(test_db_name()).

create_1st_text_att() ->
    {ok, {{_, Code, _}, _Headers, _Body}} = http:request(
        put,
        {db_url() ++ "/testdoc1/readme.txt", [],
        "text/plain", test_text_data()},
        [],
        [{sync, true}]),
    etap:is(Code, 201, "Created text attachment using the standalone api"),
    ok.

create_1st_png_att() ->
    {ok, {{_, Code, _}, _Headers, _Body}} = http:request(
        put,
        {db_url() ++ "/testdoc2/icon.png", [],
        "image/png", test_png_data()},
        [],
        [{sync, true}]),
    etap:is(Code, 201, "Created png attachment using the standalone api"),
    ok.

% create a text attachment using the non-standalone attachment api
create_2nd_text_att() ->
    DocJson = {[
        {<<"_attachments">>, {[
            {<<"readme.txt">>, {[
                {<<"content_type">>, <<"text/plain">>},
                {<<"data">>, base64:encode(test_text_data())}
            ]}
        }]}}
    ]},
    {ok, {{_, Code, _}, _Headers, _Body}} = http:request(
        put,
        {db_url() ++ "/testdoc3", [],
        "application/json", list_to_binary(couch_util:json_encode(DocJson))},
        [],
        [{sync, true}]),
    etap:is(Code, 201, "Created text attachment using the non-standalone api"),
    ok.

% create a png attachment using the non-standalone attachment api
create_2nd_png_att() ->
    DocJson = {[
        {<<"_attachments">>, {[
            {<<"icon.png">>, {[
                {<<"content_type">>, <<"image/png">>},
                {<<"data">>, base64:encode(test_png_data())}
            ]}
        }]}}
    ]},
    {ok, {{_, Code, _}, _Headers, _Body}} = http:request(
        put,
        {db_url() ++ "/testdoc4", [],
        "application/json", list_to_binary(couch_util:json_encode(DocJson))},
        [],
        [{sync, true}]),
    etap:is(Code, 201, "Created png attachment using the non-standalone api"),
    ok.

create_already_compressed_att(DocUri, AttName) ->
    {ok, {{_, Code, _}, _Headers, _Body}} = http:request(
        put,
        {DocUri ++ "/" ++ AttName, [{"Content-Encoding", "gzip"}],
        "text/plain", zlib:gzip(test_text_data())},
        [],
        [{sync, true}]),
    etap:is(
        Code,
        201,
        "Created already compressed attachment using the standalone api"
    ),
    ok.

tests_for_1st_text_att() ->
    test_get_1st_text_att_with_accept_encoding_gzip(),
    test_get_1st_text_att_without_accept_encoding_header(),
    test_get_1st_text_att_with_accept_encoding_deflate(),
    test_get_1st_text_att_with_accept_encoding_deflate_only(),
    test_get_doc_with_1st_text_att(),
    test_1st_text_att_stub().

tests_for_1st_png_att() ->
    test_get_1st_png_att_without_accept_encoding_header(),
    test_get_1st_png_att_with_accept_encoding_gzip(),
    test_get_1st_png_att_with_accept_encoding_deflate(),
    test_get_doc_with_1st_png_att(),
    test_1st_png_att_stub().

tests_for_2nd_text_att() ->
    test_get_2nd_text_att_with_accept_encoding_gzip(),
    test_get_2nd_text_att_without_accept_encoding_header(),
    test_get_doc_with_2nd_text_att(),
    test_2nd_text_att_stub().

tests_for_2nd_png_att() ->
    test_get_2nd_png_att_without_accept_encoding_header(),
    test_get_2nd_png_att_with_accept_encoding_gzip(),
    test_get_doc_with_2nd_png_att(),
    test_2nd_png_att_stub().

test_get_1st_text_att_with_accept_encoding_gzip() ->
    {ok, {{_, Code, _}, Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc1/readme.txt", [{"Accept-Encoding", "gzip"}]},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Gziped = lists:member({"content-encoding", "gzip"}, Headers),
    etap:is(Gziped, true, "received body is gziped"),
    Uncompressed = binary_to_list(zlib:gunzip(list_to_binary(Body))),
    etap:is(
        Uncompressed,
        test_text_data(),
        "received data for the 1st text attachment is ok"
    ),
    ok.

test_get_1st_text_att_without_accept_encoding_header() ->
    {ok, {{_, Code, _}, Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc1/readme.txt", []},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Gziped = lists:member({"content-encoding", "gzip"}, Headers),
    etap:is(Gziped, false, "received body is not gziped"),
    etap:is(
        Body,
        test_text_data(),
        "received data for the 1st text attachment is ok"
    ),
    ok.

test_get_1st_text_att_with_accept_encoding_deflate() ->
    {ok, {{_, Code, _}, Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc1/readme.txt", [{"Accept-Encoding", "deflate"}]},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Gziped = lists:member({"content-encoding", "gzip"}, Headers),
    etap:is(Gziped, false, "received body is not gziped"),
    Deflated = lists:member({"content-encoding", "deflate"}, Headers),
    etap:is(Deflated, false, "received body is not deflated"),
    etap:is(
        Body,
        test_text_data(),
        "received data for the 1st text attachment is ok"
    ),
    ok.

test_get_1st_text_att_with_accept_encoding_deflate_only() ->
    {ok, {{_, Code, _}, _Headers, _Body}} = http:request(
        get,
        {db_url() ++ "/testdoc1/readme.txt",
            [{"Accept-Encoding", "deflate, *;q=0"}]},
        [],
        [{sync, true}]),
    etap:is(
        Code,
        406,
        "HTTP response code is 406 for an unsupported content encoding request"
    ),
    ok.

test_get_1st_png_att_without_accept_encoding_header() ->
    {ok, {{_, Code, _}, Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc2/icon.png", []},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Gziped = lists:member({"content-encoding", "gzip"}, Headers),
    etap:is(Gziped, false, "received body is not gziped"),
    etap:is(
        Body,
        test_png_data(),
        "received data for the 1st png attachment is ok"
    ),
    ok.

test_get_1st_png_att_with_accept_encoding_gzip() ->
    {ok, {{_, Code, _}, Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc2/icon.png", [{"Accept-Encoding", "gzip"}]},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Gziped = lists:member({"content-encoding", "gzip"}, Headers),
    etap:is(Gziped, false, "received body is not gziped"),
    etap:is(
        Body,
        test_png_data(),
        "received data for the 1st png attachment is ok"
    ),
    ok.

test_get_1st_png_att_with_accept_encoding_deflate() ->
    {ok, {{_, Code, _}, Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc2/icon.png", [{"Accept-Encoding", "deflate"}]},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Deflated = lists:member({"content-encoding", "deflate"}, Headers),
    etap:is(Deflated, false, "received body is not deflated"),
    Gziped = lists:member({"content-encoding", "gzip"}, Headers),
    etap:is(Gziped, false, "received body is not gziped"),
    etap:is(
        Body,
        test_png_data(),
        "received data for the 1st png attachment is ok"
    ),
    ok.

test_get_doc_with_1st_text_att() ->
    {ok, {{_, Code, _}, _Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc1?attachments=true", []},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Json = couch_util:json_decode(Body),
    TextAttJson = couch_util:get_nested_json_value(
        Json,
        [<<"_attachments">>, <<"readme.txt">>]
    ),
    TextAttType = couch_util:get_nested_json_value(
        TextAttJson,
        [<<"content_type">>]
    ),
    TextAttData = couch_util:get_nested_json_value(
        TextAttJson,
        [<<"data">>]
    ),
    etap:is(
        TextAttType,
        <<"text/plain">>,
        "1st text attachment has type text/plain"
    ),
    %% check the attachment's data is the base64 encoding of the plain text
    %% and not the base64 encoding of the gziped plain text
    etap:is(
        TextAttData,
        base64:encode(test_text_data()),
        "1st text attachment data is properly base64 encoded"
    ),
    ok.

test_1st_text_att_stub() ->
    {ok, {{_, Code, _}, _Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc1?att_encoding_info=true", []},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Json = couch_util:json_decode(Body),
    {TextAttJson} = couch_util:get_nested_json_value(
        Json,
        [<<"_attachments">>, <<"readme.txt">>]
    ),
    TextAttLength = couch_util:get_value(<<"length">>, TextAttJson),
    etap:is(
        TextAttLength,
        length(test_text_data()),
        "1st text attachment stub length matches the uncompressed length"
    ),
    TextAttEncoding = couch_util:get_value(<<"encoding">>, TextAttJson),
    etap:is(
        TextAttEncoding,
        <<"gzip">>,
        "1st text attachment stub has the encoding field set to gzip"
    ),
    TextAttEncLength = couch_util:get_value(<<"encoded_length">>, TextAttJson),
    etap:is(
        TextAttEncLength,
        iolist_size(zlib:gzip(test_text_data())),
        "1st text attachment stub encoded_length matches the compressed length"
    ),
    ok.

test_get_doc_with_1st_png_att() ->
    {ok, {{_, Code, _}, _Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc2?attachments=true", []},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Json = couch_util:json_decode(Body),
    PngAttJson = couch_util:get_nested_json_value(
        Json,
        [<<"_attachments">>, <<"icon.png">>]
    ),
    PngAttType = couch_util:get_nested_json_value(
        PngAttJson,
        [<<"content_type">>]
    ),
    PngAttData = couch_util:get_nested_json_value(
        PngAttJson,
        [<<"data">>]
    ),
    etap:is(PngAttType, <<"image/png">>, "attachment has type image/png"),
    etap:is(
        PngAttData,
        base64:encode(test_png_data()),
        "1st png attachment data is properly base64 encoded"
    ),
    ok.

test_1st_png_att_stub() ->
    {ok, {{_, Code, _}, _Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc2?att_encoding_info=true", []},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Json = couch_util:json_decode(Body),
    {PngAttJson} = couch_util:get_nested_json_value(
        Json,
        [<<"_attachments">>, <<"icon.png">>]
    ),
    PngAttLength = couch_util:get_value(<<"length">>, PngAttJson),
    etap:is(
        PngAttLength,
        length(test_png_data()),
        "1st png attachment stub length matches the uncompressed length"
    ),
    PngEncoding = couch_util:get_value(<<"encoding">>, PngAttJson),
    etap:is(
        PngEncoding,
        undefined,
        "1st png attachment stub doesn't have an encoding field"
    ),
    PngEncLength = couch_util:get_value(<<"encoded_length">>, PngAttJson),
    etap:is(
        PngEncLength,
        undefined,
        "1st png attachment stub doesn't have an encoded_length field"
    ),
    ok.

test_get_2nd_text_att_with_accept_encoding_gzip() ->
    {ok, {{_, Code, _}, Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc3/readme.txt", [{"Accept-Encoding", "gzip"}]},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Gziped = lists:member({"content-encoding", "gzip"}, Headers),
    etap:is(Gziped, true, "received body is gziped"),
    Uncompressed = binary_to_list(zlib:gunzip(list_to_binary(Body))),
    etap:is(
        Uncompressed,
        test_text_data(),
        "received data for the 2nd text attachment is ok"
    ),
    ok.

test_get_2nd_text_att_without_accept_encoding_header() ->
    {ok, {{_, Code, _}, Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc3/readme.txt", []},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Gziped = lists:member({"content-encoding", "gzip"}, Headers),
    etap:is(Gziped, false, "received body is not gziped"),
    etap:is(
        Body,
        test_text_data(),
        "received data for the 2nd text attachment is ok"
    ),
    ok.

test_get_2nd_png_att_without_accept_encoding_header() ->
    {ok, {{_, Code, _}, Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc4/icon.png", []},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Gziped = lists:member({"content-encoding", "gzip"}, Headers),
    etap:is(Gziped, false, "received body is not gziped"),
    etap:is(
        Body,
        test_png_data(),
        "received data for the 2nd png attachment is ok"
    ),
    ok.

test_get_2nd_png_att_with_accept_encoding_gzip() ->
    {ok, {{_, Code, _}, Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc4/icon.png", [{"Accept-Encoding", "gzip"}]},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Gziped = lists:member({"content-encoding", "gzip"}, Headers),
    etap:is(Gziped, false, "received body is not gziped"),
    etap:is(
        Body,
        test_png_data(),
        "received data for the 2nd png attachment is ok"
    ),
    ok.

test_get_doc_with_2nd_text_att() ->
    {ok, {{_, Code, _}, _Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc3?attachments=true", []},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Json = couch_util:json_decode(Body),
    TextAttJson = couch_util:get_nested_json_value(
        Json,
        [<<"_attachments">>, <<"readme.txt">>]
    ),
    TextAttType = couch_util:get_nested_json_value(
        TextAttJson,
        [<<"content_type">>]
    ),
    TextAttData = couch_util:get_nested_json_value(
        TextAttJson,
        [<<"data">>]
    ),
    etap:is(TextAttType, <<"text/plain">>, "attachment has type text/plain"),
    %% check the attachment's data is the base64 encoding of the plain text
    %% and not the base64 encoding of the gziped plain text
    etap:is(
        TextAttData,
        base64:encode(test_text_data()),
        "2nd text attachment data is properly base64 encoded"
    ),
    ok.

test_2nd_text_att_stub() ->
    {ok, {{_, Code, _}, _Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc3?att_encoding_info=true", []},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Json = couch_util:json_decode(Body),
    {TextAttJson} = couch_util:get_nested_json_value(
        Json,
        [<<"_attachments">>, <<"readme.txt">>]
    ),
    TextAttLength = couch_util:get_value(<<"length">>, TextAttJson),
    etap:is(
        TextAttLength,
        length(test_text_data()),
        "2nd text attachment stub length matches the uncompressed length"
    ),
    TextAttEncoding = couch_util:get_value(<<"encoding">>, TextAttJson),
    etap:is(
        TextAttEncoding,
        <<"gzip">>,
        "2nd text attachment stub has the encoding field set to gzip"
    ),
    TextAttEncLength = couch_util:get_value(<<"encoded_length">>, TextAttJson),
    etap:is(
        TextAttEncLength,
        iolist_size(zlib:gzip(test_text_data())),
        "2nd text attachment stub encoded_length matches the compressed length"
    ),
    ok.

test_get_doc_with_2nd_png_att() ->
    {ok, {{_, Code, _}, _Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc4?attachments=true", []},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Json = couch_util:json_decode(Body),
    PngAttJson = couch_util:get_nested_json_value(
        Json,
        [<<"_attachments">>, <<"icon.png">>]
    ),
    PngAttType = couch_util:get_nested_json_value(
        PngAttJson,
        [<<"content_type">>]
    ),
    PngAttData = couch_util:get_nested_json_value(
        PngAttJson,
        [<<"data">>]
    ),
    etap:is(PngAttType, <<"image/png">>, "attachment has type image/png"),
    etap:is(
        PngAttData,
        base64:encode(test_png_data()),
        "2nd png attachment data is properly base64 encoded"
    ),
    ok.

test_2nd_png_att_stub() ->
    {ok, {{_, Code, _}, _Headers, Body}} = http:request(
        get,
        {db_url() ++ "/testdoc4?att_encoding_info=true", []},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Json = couch_util:json_decode(Body),
    {PngAttJson} = couch_util:get_nested_json_value(
        Json,
        [<<"_attachments">>, <<"icon.png">>]
    ),
    PngAttLength = couch_util:get_value(<<"length">>, PngAttJson),
    etap:is(
        PngAttLength,
        length(test_png_data()),
        "2nd png attachment stub length matches the uncompressed length"
    ),
    PngEncoding = couch_util:get_value(<<"encoding">>, PngAttJson),
    etap:is(
        PngEncoding,
        undefined,
        "2nd png attachment stub doesn't have an encoding field"
    ),
    PngEncLength = couch_util:get_value(<<"encoded_length">>, PngAttJson),
    etap:is(
        PngEncLength,
        undefined,
        "2nd png attachment stub doesn't have an encoded_length field"
    ),
    ok.

test_already_compressed_att(DocUri, AttName) ->
    test_get_already_compressed_att_with_accept_gzip(DocUri, AttName),
    test_get_already_compressed_att_without_accept(DocUri, AttName),
    test_get_already_compressed_att_stub(DocUri, AttName).

test_get_already_compressed_att_with_accept_gzip(DocUri, AttName) ->
    {ok, {{_, Code, _}, Headers, Body}} = http:request(
        get,
        {DocUri ++ "/" ++ AttName, [{"Accept-Encoding", "gzip"}]},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Gziped = lists:member({"content-encoding", "gzip"}, Headers),
    etap:is(Gziped, true, "received body is gziped"),
    etap:is(
        iolist_to_binary(Body),
        iolist_to_binary(zlib:gzip(test_text_data())),
        "received data for the already compressed attachment is ok"
    ),
    ok.

test_get_already_compressed_att_without_accept(DocUri, AttName) ->
    {ok, {{_, Code, _}, Headers, Body}} = http:request(
        get,
        {DocUri ++ "/" ++ AttName, []},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Gziped = lists:member({"content-encoding", "gzip"}, Headers),
    etap:is(Gziped, false, "received body is not gziped"),
    etap:is(
        iolist_to_binary(Body),
        iolist_to_binary(test_text_data()),
        "received data for the already compressed attachment is ok"
    ),
    ok.

test_get_already_compressed_att_stub(DocUri, AttName) ->
    {ok, {{_, Code, _}, _Headers, Body}} = http:request(
        get,
        {DocUri ++ "?att_encoding_info=true", []},
        [],
        [{sync, true}]),
    etap:is(Code, 200, "HTTP response code is 200"),
    Json = couch_util:json_decode(Body),
    {AttJson} = couch_util:get_nested_json_value(
        Json,
        [<<"_attachments">>, iolist_to_binary(AttName)]
    ),
    AttLength = couch_util:get_value(<<"length">>, AttJson),
    etap:is(
        AttLength,
        iolist_size((zlib:gzip(test_text_data()))),
        "Already compressed attachment stub length matches the "
        "compressed length"
    ),
    Encoding = couch_util:get_value(<<"encoding">>, AttJson),
    etap:is(
        Encoding,
        <<"gzip">>,
        "Already compressed attachment stub has the encoding field set to gzip"
    ),
    EncLength = couch_util:get_value(<<"encoded_length">>, AttJson),
    etap:is(
        EncLength,
        AttLength,
        "Already compressed attachment stub encoded_length matches the "
        "length field value"
    ),
    ok.

test_create_already_compressed_att_with_invalid_content_encoding(
    DocUri, AttName, AttData, Encoding) ->
    {ok, {{_, Code, _}, _Headers, _Body}} = http:request(
        put,
        {DocUri ++ "/" ++ AttName, [{"Content-Encoding", Encoding}],
        "text/plain", AttData},
        [],
        [{sync, true}]),
    etap:is(
        Code,
        415,
        "Couldn't create an already compressed attachment using the "
        "unsupported encoding '" ++ Encoding ++ "'"
    ),
    ok.

test_png_data() ->
    {ok, Data} = file:read_file(
        test_util:source_file("share/www/image/logo.png")
    ),
    binary_to_list(Data).

test_text_data() ->
    {ok, Data} = file:read_file(
        test_util:source_file("README")
    ),
    binary_to_list(Data).