Perforation can be diagnosed by an upper GI study, a test in which x-rays are taken after the person has swallowed a thick dye. If the healthcare provider suspects a perforation, endoscopy may be done first. An endoscopy involves the insertion of a thin tube through which the doctor can see the walls of the esophagus.