test/jdk/java/net/httpclient/http2/jdk.incubator.httpclient/jdk/incubator/http/internal/hpack/EncoderTest.java
branchhttp-client-branch
changeset 55763 634d8e14c172
parent 47216 71c04702a3d5
equal deleted inserted replaced
55762:e947a3a50a95 55763:634d8e14c172
     1 /*
     1 /*
     2  * Copyright (c) 2014, 2016, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2014, 2017, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    22  */
    22  */
    23 package jdk.incubator.http.internal.hpack;
    23 package jdk.incubator.http.internal.hpack;
    24 
    24 
    25 import org.testng.annotations.Test;
    25 import org.testng.annotations.Test;
    26 
    26 
       
    27 import java.io.IOException;
    27 import java.nio.Buffer;
    28 import java.nio.Buffer;
    28 import java.nio.ByteBuffer;
    29 import java.nio.ByteBuffer;
    29 import java.util.ArrayList;
    30 import java.util.ArrayList;
    30 import java.util.Collections;
    31 import java.util.Collections;
    31 import java.util.Iterator;
    32 import java.util.Iterator;
   518              "      Table size: 215");
   519              "      Table size: 215");
   519         // @formatter:on
   520         // @formatter:on
   520     }
   521     }
   521 
   522 
   522     @Test
   523     @Test
   523     public void initialSizeUpdateDefaultEncoder() {
   524     public void initialSizeUpdateDefaultEncoder() throws IOException {
   524         Function<Integer, Encoder> e = Encoder::new;
   525         Function<Integer, Encoder> e = Encoder::new;
   525         testSizeUpdate(e, 1024, asList(), asList(0));
   526         testSizeUpdate(e, 1024, asList(), asList(0));
   526         testSizeUpdate(e, 1024, asList(1024), asList(0));
   527         testSizeUpdate(e, 1024, asList(1024), asList(0));
   527         testSizeUpdate(e, 1024, asList(1024, 1024), asList(0));
   528         testSizeUpdate(e, 1024, asList(1024, 1024), asList(0));
   528         testSizeUpdate(e, 1024, asList(1024, 512), asList(0));
   529         testSizeUpdate(e, 1024, asList(1024, 512), asList(0));
   529         testSizeUpdate(e, 1024, asList(512, 1024), asList(0));
   530         testSizeUpdate(e, 1024, asList(512, 1024), asList(0));
   530         testSizeUpdate(e, 1024, asList(512, 2048), asList(0));
   531         testSizeUpdate(e, 1024, asList(512, 2048), asList(0));
   531     }
   532     }
   532 
   533 
   533     @Test
   534     @Test
   534     public void initialSizeUpdateCustomEncoder() {
   535     public void initialSizeUpdateCustomEncoder() throws IOException {
   535         Function<Integer, Encoder> e = EncoderTest::newCustomEncoder;
   536         Function<Integer, Encoder> e = EncoderTest::newCustomEncoder;
   536         testSizeUpdate(e, 1024, asList(), asList(1024));
   537         testSizeUpdate(e, 1024, asList(), asList(1024));
   537         testSizeUpdate(e, 1024, asList(1024), asList(1024));
   538         testSizeUpdate(e, 1024, asList(1024), asList(1024));
   538         testSizeUpdate(e, 1024, asList(1024, 1024), asList(1024));
   539         testSizeUpdate(e, 1024, asList(1024, 1024), asList(1024));
   539         testSizeUpdate(e, 1024, asList(1024, 512), asList(512));
   540         testSizeUpdate(e, 1024, asList(1024, 512), asList(512));
   540         testSizeUpdate(e, 1024, asList(512, 1024), asList(1024));
   541         testSizeUpdate(e, 1024, asList(512, 1024), asList(1024));
   541         testSizeUpdate(e, 1024, asList(512, 2048), asList(2048));
   542         testSizeUpdate(e, 1024, asList(512, 2048), asList(2048));
   542     }
   543     }
   543 
   544 
   544     @Test
   545     @Test
   545     public void seriesOfSizeUpdatesDefaultEncoder() {
   546     public void seriesOfSizeUpdatesDefaultEncoder() throws IOException {
   546         Function<Integer, Encoder> e = c -> {
   547         Function<Integer, Encoder> e = c -> {
   547             Encoder encoder = new Encoder(c);
   548             Encoder encoder = new Encoder(c);
   548             drainInitialUpdate(encoder);
   549             drainInitialUpdate(encoder);
   549             return encoder;
   550             return encoder;
   550         };
   551         };
   561 
   562 
   562     //
   563     //
   563     // https://tools.ietf.org/html/rfc7541#section-4.2
   564     // https://tools.ietf.org/html/rfc7541#section-4.2
   564     //
   565     //
   565     @Test
   566     @Test
   566     public void seriesOfSizeUpdatesCustomEncoder() {
   567     public void seriesOfSizeUpdatesCustomEncoder() throws IOException {
   567         Function<Integer, Encoder> e = c -> {
   568         Function<Integer, Encoder> e = c -> {
   568             Encoder encoder = newCustomEncoder(c);
   569             Encoder encoder = newCustomEncoder(c);
   569             drainInitialUpdate(encoder);
   570             drainInitialUpdate(encoder);
   570             return encoder;
   571             return encoder;
   571         };
   572         };
   636     // updates      - a sequence of values for consecutive calls to encoder.setMaxCapacity
   637     // updates      - a sequence of values for consecutive calls to encoder.setMaxCapacity
   637     // expected     - a sequence of values expected to be decoded by a decoder
   638     // expected     - a sequence of values expected to be decoded by a decoder
   638     private void testSizeUpdate(Function<Integer, Encoder> encoder,
   639     private void testSizeUpdate(Function<Integer, Encoder> encoder,
   639                                 int initialSize,
   640                                 int initialSize,
   640                                 List<Integer> updates,
   641                                 List<Integer> updates,
   641                                 List<Integer> expected) {
   642                                 List<Integer> expected) throws IOException {
   642         Encoder e = encoder.apply(initialSize);
   643         Encoder e = encoder.apply(initialSize);
   643         updates.forEach(e::setMaxCapacity);
   644         updates.forEach(e::setMaxCapacity);
   644         ByteBuffer b = ByteBuffer.allocate(64);
   645         ByteBuffer b = ByteBuffer.allocate(64);
   645         e.header("a", "b");
   646         e.header("a", "b");
   646         e.encode(b);
   647         e.encode(b);