HpackDecodeTestBase.java revision 71b9f47b26fb57ac3e436a19519c6e3ec70e86eb
1/*
2 * Copyright (C) 2014 Square, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package com.squareup.okhttp.internal.framed;
17
18import com.squareup.okhttp.internal.framed.hpackjson.Case;
19import com.squareup.okhttp.internal.framed.hpackjson.HpackJsonUtil;
20import com.squareup.okhttp.internal.framed.hpackjson.Story;
21import java.util.ArrayList;
22import java.util.Collection;
23import java.util.LinkedHashSet;
24import java.util.List;
25import okio.Buffer;
26
27import static org.junit.Assert.assertEquals;
28import static org.junit.Assert.fail;
29
30/**
31 * Tests Hpack implementation using https://github.com/http2jp/hpack-test-case/
32 */
33public class HpackDecodeTestBase {
34
35  /**
36   * Reads all stories in the folders provided, asserts if no story found.
37   */
38  protected static Collection<Story[]> createStories(String[] interopTests)
39      throws Exception {
40    List<Story[]> result = new ArrayList<>();
41    for (String interopTestName : interopTests) {
42      List<Story> stories = HpackJsonUtil.readStories(interopTestName);
43      if (stories.isEmpty()) {
44        fail("No stories for: " + interopTestName);
45      }
46      for (Story story : stories) {
47        result.add(new Story[] { story });
48      }
49    }
50    return result;
51  }
52
53  private final Buffer bytesIn = new Buffer();
54  private final Hpack.Reader hpackReader = new Hpack.Reader(4096, bytesIn);
55
56  private final Story story;
57
58  public HpackDecodeTestBase(Story story) {
59    this.story = story;
60  }
61
62  /**
63   * Expects wire to be set for all cases, and compares the decoder's output to
64   * expected headers.
65   */
66  protected void testDecoder() throws Exception {
67    testDecoder(story);
68  }
69
70  protected void testDecoder(Story story) throws Exception {
71    for (Case caze : story.getCases()) {
72      bytesIn.write(caze.getWire());
73      hpackReader.readHeaders();
74      assertSetEquals(String.format("seqno=%d", caze.getSeqno()), caze.getHeaders(),
75          hpackReader.getAndResetHeaderList());
76    }
77  }
78  /**
79   * Checks if {@code expected} and {@code observed} are equal when viewed as a
80   * set and headers are deduped.
81   *
82   * TODO: See if duped headers should be preserved on decode and verify.
83   */
84  private static void assertSetEquals(
85      String message, List<Header> expected, List<Header> observed) {
86    assertEquals(message, new LinkedHashSet<>(expected), new LinkedHashSet<>(observed));
87  }
88
89  protected Story getStory() {
90    return story;
91  }
92}
93