<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.
 */
package org.apache.shindig.gadgets.http;

import junit.framework.TestCase;

import java.net.URI;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

/**
 * Tests for basic content cache
 */
public class BasicHttpCacheTest extends TestCase {
  /**
   * Used to parse Expires: header.
   */
  private final static DateFormat dateFormat
      = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z", Locale.US);
  static {
    dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
  }

  private HttpCache cache;

  @Override
  public void setUp() throws Exception {
    super.setUp();
    cache = new BasicHttpCache();
  }

  @Override
  protected void tearDown() throws Exception {
    cache = null;
    super.tearDown();
  }

  private HttpRequest createRequest(String method) {
    HttpRequest req = new HttpRequest(method,
        URI.create("http://www.here.com"), new HashMap<String, List<String>>(),
        new byte[0], new HttpRequest.Options());
    return req;
  }

  private HttpResponse createResponse(int statusCode, String header,
      String headerValue) {
    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    if (header != null) {
      headers.put(header, Arrays.asList(headerValue));
    }
    HttpResponse resp = new HttpResponse(statusCode, new byte[0], headers);
    return resp;
  }

  private HttpResponse createExpiresResponse(int statusCode, long expiration) {
    Date newExpiry = new Date(expiration);
    return createResponse(statusCode, "Expires", dateFormat.format(newExpiry));
  }

  private HttpResponse createMaxAgeResponse(int statusCode, long age) {
    return createResponse(statusCode, "Cache-Control", "max-age=" + age);
  }

  public void testEmptyCache() {
    assertNull(cache.getResponse(createRequest("GET")));
  }

  public void testCacheable() {
    HttpRequest req = createRequest("GET");
    HttpResponse resp = createResponse(200, null, null);
    cache.addResponse(req, resp);
    assertEquals(cache.getResponse(req), resp);
  }

  public void testNotCacheableForPost() {
    HttpRequest req = createRequest("POST");
    HttpResponse resp = createResponse(200, null, null);
    cache.addResponse(req, resp);
    assertNull(cache.getResponse(req));<commit_msg>Simplify BasicHttpCacheTest to use HttpUtil to parse Expiry date<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements. See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership. The ASF licenses this file
 * to you 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.
 */
package org.apache.shindig.gadgets.http;

import junit.framework.TestCase;

import org.apache.shindig.gadgets.servlet.HttpUtil;

import java.net.URI;
import java.text.DateFormat;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.TimeZone;

/**
 * Tests for basic content cache
 */
public class BasicHttpCacheTest extends TestCase {

  private HttpCache cache;

  @Override
  public void setUp() throws Exception {
    super.setUp();
    cache = new BasicHttpCache();
  }

  @Override
  protected void tearDown() throws Exception {
    cache = null;
    super.tearDown();
  }

  private HttpRequest createRequest(String method) {
    HttpRequest req = new HttpRequest(method,
        URI.create("http://www.here.com"), new HashMap<String, List<String>>(),
        new byte[0], new HttpRequest.Options());
    return req;
  }

  private HttpResponse createResponse(int statusCode, String header,
      String headerValue) {
    Map<String, List<String>> headers = new HashMap<String, List<String>>();
    if (header != null) {
      headers.put(header, Arrays.asList(headerValue));
    }
    HttpResponse resp = new HttpResponse(statusCode, new byte[0], headers);
    return resp;
  }

  private HttpResponse createExpiresResponse(int statusCode, long expiration) {
    Date newExpiry = new Date(expiration);
    return createResponse(statusCode, "Expires", HttpUtil.formatDate(newExpiry));
  }

  private HttpResponse createMaxAgeResponse(int statusCode, long age) {
    return createResponse(statusCode, "Cache-Control", "max-age=" + age);
  }

  public void testEmptyCache() {
    assertNull(cache.getResponse(createRequest("GET")));
  }

  public void testCacheable() {
    HttpRequest req = createRequest("GET");
    HttpResponse resp = createResponse(200, null, null);
    cache.addResponse(req, resp);
    assertEquals(cache.getResponse(req), resp);
  }

  public void testNotCacheableForPost() {
    HttpRequest req = createRequest("POST");
    HttpResponse resp = createResponse(200, null, null);
    cache.addResponse(req, resp);
    assertNull(cache.getResponse(req));
{'fraction_non_alphanumeric': 0.10138029509757258, 'fraction_numerical': 0.0038077106139933364, 'mean_word_length': 4.129373474369406, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '683089', 'n_tokens_mistral': 1771, 'n_tokens_neox': 1739, 'n_words': 573}