All Downloads are FREE. Search and download functionalities are using the official Maven repository.

commonTest.okhttp3.MediaTypeTest.kt Maven / Gradle / Ivy

There is a newer version: 5.0.0-alpha.14
Show newest version
/*
 * Copyright (C) 2013 Square, Inc.
 * Copyright (C) 2011 The Guava Authors
 *
 * 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.
 */
package okhttp3

import kotlin.test.Test
import kotlin.test.assertEquals
import kotlin.test.assertNull
import okhttp3.MediaType.Companion.toMediaTypeOrNull
import okhttp3.internal.uppercase

/**
 * Test MediaType API and parsing.
 *
 * This test includes tests from [Guava's](https://code.google.com/p/guava-libraries/)
 * MediaTypeTest.
 */
open class MediaTypeTest {
  open fun MediaType.charsetName(): String? = parameter("charset")?.uppercase()

  protected open fun parse(string: String): MediaType = string.toMediaTypeOrNull()!!

  protected open fun assertInvalid(string: String, exceptionMessage: String?) {
    assertNull(string.toMediaTypeOrNull(), exceptionMessage)
  }

  @Test fun testParse() {
    val mediaType = parse("text/plain;boundary=foo;charset=utf-8")
    assertEquals("text", mediaType.type)
    assertEquals("plain", mediaType.subtype)
    assertEquals("UTF-8", mediaType.charsetName())
    assertEquals("text/plain;boundary=foo;charset=utf-8", mediaType.toString())
    assertEquals(mediaType, parse("text/plain;boundary=foo;charset=utf-8"))
    assertEquals(
      mediaType.hashCode(),
      parse("text/plain;boundary=foo;charset=utf-8").hashCode()
    )
  }

  @Test fun testValidParse() {
    assertMediaType("text/plain")
    assertMediaType("application/atom+xml; charset=utf-8")
    assertMediaType("application/atom+xml; a=1; a=2; b=3")
    assertMediaType("image/gif; foo=bar")
    assertMediaType("text/plain; a=1")
    assertMediaType("text/plain; a=1; a=2; b=3")
    assertMediaType("text/plain; charset=utf-16")
    assertMediaType("text/plain; \t \n \r a=b")
    assertMediaType("text/plain;")
    assertMediaType("text/plain; ")
    assertMediaType("text/plain; a=1;")
    assertMediaType("text/plain; a=1; ")
    assertMediaType("text/plain; a=1;; b=2")
    assertMediaType("text/plain;;")
    assertMediaType("text/plain; ;")
  }

  @Test fun testInvalidParse() {
    assertInvalid("", "No subtype found for: \"\"")
    assertInvalid("/", "No subtype found for: \"/\"")
    assertInvalid("text", "No subtype found for: \"text\"")
    assertInvalid("text/", "No subtype found for: \"text/\"")
    assertInvalid("te




© 2015 - 2024 Weber Informatics LLC | Privacy Policy